I get this error:
ArgumentException: The thing you want to instantiate is null.
UnityEngine.Object.CheckNullArgument (System.Object arg, System.String
message)
UnityEngine.Object.Instantiate (UnityEngine.Object original)
ScoreHandler.OnCollisionEnter (UnityEngine.Collision col) (at
Assets/ScoreHandler.cs:92)
My objects are named 0, 1, 2, 3, ..., and 9. I turn the score into a string and then instantiate the gameobject with the corresponding title.
As I said, this all works perfectly. Why am I getting this error?
Note that ScoreHandler is the name of my script.
The thing is that all of my objects are instantiated as they should be.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class ScoreHandler : MonoBehaviour {
public int score = 0;
public List<GameObject> destroyList = new List<GameObject>();
public static GameObject[] Score;
// Use this for initialization
void Start () {
score -= 80;
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter (Collision col)
{
if (col.gameObject.name == "carA") {
score += 10;
}
if(col.gameObject.name == "carB")
{
score += 10;
}
if(col.gameObject.name == "carC")
{
score += 10;
}
if(col.gameObject.name == "carD")
{
score += 10;
}
if(col.gameObject.name == "carE")
{
score += 10;
}
if(col.gameObject.name == "carF")
{
score += 10;
}
if(col.gameObject.name == "carG")
{
score += 10;
}
if(col.gameObject.name == "carH")
{
score += 10;
}
foreach (var go in destroyList)
{
Destroy(go);
}
destroyList.Clear();
string scoreText = score.ToString ();
Score = new GameObject[scoreText.Length];
for (int i = 0; i < scoreText.Length; i++) {
Score[i] = (GameObject)Instantiate (Resources.Load (scoreText
[i].ToString ()));
Score[i].layer = 8;
Score[i].transform.localScale = new Vector3 (0.02F, 0.02F,
0.02F);
Score[i].transform.localPosition = new Vector3 (0.013F + i *
0.01F, 0.12F, 0.0F);
Score[i].transform.Rotate (0, 180, 0);
destroyList.Add (Score[i]);
}
}
}