0

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]);



        }
    }

}
  • why do you convert the score to string? assuming score is 10, that would make your cycle go through 2 iterations, for values 1 and 0. And since you start with score -80, the first iteration would be either -80 or -70, so your loop would go through 3 iterations, -, 8 and 0 strings.. so `Score[i] = (GameObject)Instantiate (Resources.Load (scoreText [i].ToString ()));` would most likely be null, because there probably is no resource to be loaded with `-` ID – Jiri P. Jan 02 '16 at 01:53
  • Cannot apply indexing with [] to an expression of type int –  Jan 02 '16 at 01:59
  • @Jiri P. you seem very knowledgable on this topic. Can you help me get rid of this error in my specific context? –  Jan 02 '16 at 02:00
  • @Jiri P. I get what you are saying. The score starts at 80 so to counteract that I quickly added -80 at the start. So it does instantiate the 0 object at the start. I guess like you say it tries to instantiate - and cant find it. I will add an object called - and see if that solves my problem –  Jan 02 '16 at 02:02
  • Wow you are so smart I added a "-" GameObject to my resources folder and I don't get an error anymore. I would have never thought of that. Thank you very much! –  Jan 02 '16 at 02:08

0 Answers0