0

Null reference exceptions occur when I try to reference an array gameObject from my levelGen script, not sure how to fix it as everything is set correctly in inspector

I've tried referencing gameObject's int in the array, directly calling it and messing around with different iterations of my instantiate script but can't seem to get it working.

LevelGen script

    public GameObject[] blockPrefabs;
public GameObject[] blocks;
public int blockInt;
public GameObject blockInstance;
// Use this for initialization
void Start () {

    //blocks = new GameObject[blockPrefabs.Length];
    //for (int b = 0; b < blockPrefabs.Length; b++) {
        //for (int y = 0; y < 5; y++) {
            //for (int i = 0; i < 5; i++) {

                //Duplicates square in each colour
                //blocks [b] = (blockPrefabs 
  [b].gameObject); Random.Range (0,blockPrefabs.Length);
                //blocks [b] = Instantiate(blocks [b], 
  new Vector3 (i * 5.0f, y * 5.0f, 0), transform.rotation);

            for (int y = 0; y < 5; y++) {
                for (int i = 0; i < 5; i++) {
            blockInt = Random.Range (0, 
  blockPrefabs.Length);
            Instantiate (blockPrefabs [blockInt], new 
  Vector3 (i * 5.0f, y * 5.0f, 0), transform.rotation);
                Debug.Log (blockPrefabs);

Raycasting script

 if (Input.GetButtonDown ("Fire1")) {

        Ray raycast = Camera.main.ScreenPointToRay 
 (Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast (raycast, out hit)) {

            if ((hit.collider != null) && 
 (hit.collider.gameObject == levelgen.blockPrefabs[0])) {
                    redselect.redchange = true;
                    Debug.Log ("red clicked");
                    combinations ();
                } else {
                    Debug.Log ("failed red");
                }   

Be able to click on the instantiated object to run the rest of my code. At the moment when I click on it it returns null references.

  • 3
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Sir Rufo Jul 26 '19 at 05:46
  • 1
    Could you first please fix the code in your question? If code is commented out so not relevant for the question rather remove it completely.. currently it is a bit cluttered by the line breaks and wouldn't even compile ... Are you really sure all arrays are filled correctly in the Inspector? – derHugo Jul 26 '19 at 07:52
  • 1
    In which line exactly is the exception thrown? Since you say it happens when you click it seems that it jad nothing to do with the `Start` method... Where do `levelgen` and `redselect` come from? Please add a complete code. Also note that `blockPrefabs` holds Prefabs not Instantiated objects from the scene => a check between a Scene reference and a prefab in `hit.collider.gameObject == levelgen.blockPrefabs[0])` will **never** be true. – derHugo Jul 26 '19 at 07:58
  • Hey derHugo, I left the code in on the off chance someone saw a workable solution in the other code attempts, anything in between start and for int y should be commented out. I also realised blockint should be blocks instead of blockPrefabs, as that gets my randomised levels. Only blockPrefab array is filled in, other array is left blank. – user3211493 Jul 27 '19 at 12:46

1 Answers1

0

in your case, null reference error occurs when the blockInt value is out ranged than the Object array. you can handle it in two way:

  1. check on blockInt value i.e. if(blockInt< blockPrefabs.Length)
  2. check for null reference i.e. if(blockPrefabs [blockInt]!=null)

Hope this will solve your issue!

Saeleas
  • 538
  • 2
  • 8