Search Unity

Need help with null reference exception

Discussion in 'Scripting' started by Fuzzytron, Mar 7, 2018.

  1. Fuzzytron

    Fuzzytron

    Joined:
    Feb 22, 2018
    Posts:
    13
    Hi guys, hope this is the right place to post this, I see others with similar problems but can't seem to figure that out in terms of my code. I am trying to make a knife spawn on the right hand side of the right hand side of the screen.

    I have the following error:

    NullReferenceException: Object reference not set to an instance of an object
    Parallaxer.Shift () (at Assets/scripts/Parallaxer.cs:134)
    Parallaxer.Update () (at Assets/scripts/Parallaxer.cs:88)

    This is my code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Parallaxer : MonoBehaviour
    6. {
    7.  
    8.     class PoolObject
    9.     {
    10.         public Transform transform;
    11.         public bool inUse;
    12.         public PoolObject(Transform t)
    13.         {
    14.             transform = t;
    15.         }
    16.         public void Use()
    17.         {
    18.             inUse = true;
    19.         }
    20.         public void Dispose()
    21.         {
    22.             inUse = false;
    23.         }
    24.     }
    25.  
    26.     [System.Serializable]
    27.     public struct YSpawnRange
    28.     {
    29.         public float min;
    30.         public float max;
    31.     }
    32.  
    33.  
    34.     public GameObject Prefab;
    35.     public int poolSize;
    36.     public float shiftSpeed; //how fast the object is moving
    37.     public float spawnRate; //how often it spawns
    38.  
    39.     public YSpawnRange ySpawnRange;
    40.     public Vector3 defaultSpawnPos; //its starting position
    41.     public bool spawnImmediate; //whether it will spawn immediately
    42.     public Vector3 immediateSpawnPos; //the immediate spawn position
    43.     public Vector2 targetAspectRatio; //target aspect ratio, accessibility stuff
    44.  
    45.     float spawnTimer;
    46.     float targetAspect;
    47.     PoolObject[] poolObjects;
    48.     GameManager game;
    49.  
    50.     void Awake() //for initialisation
    51.     {
    52.  
    53.     }
    54.  
    55.     void Start() //initialisate game
    56.     {
    57.         game = GameManager.Instance;
    58.     }
    59.  
    60.     void OnEnable()
    61.     {
    62.         GameManager.GameOverConfirmed += GameOverConfirmed;
    63.     }
    64.  
    65.     void OnDisable()
    66.     {
    67.         GameManager.GameOverConfirmed -= GameOverConfirmed;
    68.     }
    69.  
    70.     void GameOverConfirmed()
    71.     {
    72.         for(int i = 0; i < poolObjects.Length; i++)
    73.         {
    74.             poolObjects[i].Dispose();
    75.             poolObjects[i].transform.position = Vector3.one * 1000; //off screen
    76.         }
    77.  
    78.         if (spawnImmediate)
    79.         {
    80.             SpawnImmediate();
    81.         }
    82.     }
    83.  
    84.     void Update()
    85.     {
    86.         if (game.GameOver) return;
    87.  
    88.         Shift();
    89.         spawnTimer += Time.deltaTime;
    90.         if (spawnTimer > spawnRate)
    91.         {
    92.             Spawn();
    93.             spawnTimer = 0;
    94.         }
    95.     }
    96.  
    97.     void Configure()
    98.     {
    99.         targetAspect = targetAspectRatio.x / targetAspectRatio.y;
    100.         poolObjects = new PoolObject[poolSize];
    101.         for (int i = 0; i < poolObjects.Length; i++)
    102.         {
    103.             GameObject go = Instantiate(Prefab) as GameObject;
    104.             Transform t = go.transform;
    105.             t.SetParent(transform); //has the parent of object script is linked to
    106.             t.position = Vector3.one * 1000; //offscreen again to avoid user seeing it
    107.             poolObjects[i] = new PoolObject(t);
    108.         }
    109.     }
    110.  
    111.     void Spawn()
    112.     {
    113.         Transform t = GetPoolObject();
    114.         if (t == null) return; //true means pool size is too small
    115.         Vector3 pos = Vector3.zero;
    116.         pos.x = defaultSpawnPos.x;
    117.         pos.y = Random.Range(ySpawnRange.min, ySpawnRange.max);
    118.         t.position = pos;
    119.     }
    120.  
    121.     void SpawnImmediate()//two objects to avoid gaps between objects
    122.     {
    123.         Transform t = GetPoolObject();
    124.         if (t == null) return; //true means pool size is too small
    125.         Vector3 pos = Vector3.zero;
    126.         pos.x = immediateSpawnPos.x;
    127.         pos.y = Random.Range(ySpawnRange.min, ySpawnRange.max);
    128.         t.position = pos;
    129.         Spawn();
    130.     }
    131.  
    132.     void Shift()
    133.     {
    134.         for (int i = 0; i < poolObjects.Length; i++)
    135.         {
    136.             poolObjects[i].transform.localPosition += -Vector3.right * shiftSpeed * Time.deltaTime;
    137.             CheckDisposeObject(poolObjects[i]); //checking if the position is less than spawn position
    138.         }
    139.     }
    140.  
    141.     void CheckDisposeObject(PoolObject poolObject)
    142.     {
    143.         if (poolObject.transform.position.x < -defaultSpawnPos.x) //should spawn offscreen so assume negative x is out of sight so can be removed
    144.         {
    145.             poolObject.Dispose();
    146.             poolObject.transform.position = Vector3.one * 1000; //high value the user wont see
    147.         }
    148.     }
    149.  
    150.     Transform GetPoolObject()
    151.     {
    152.         for (int i = 0; i < poolObjects.Length; i++)
    153.         {
    154.             if (!poolObjects[i].inUse)
    155.             {
    156.                 poolObjects[i].Use();
    157.                 return poolObjects[i].transform;
    158.             }
    159.         }
    160.         return null;
    161.     }
    162. }
    163.  
    Here are my inspector screens etc:
    upload_2018-3-7_13-47-24.png

    upload_2018-3-7_13-47-41.png

    Anyone got any ideas?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you instantiate the array "pooledObjects" in Configure... you dont appear to ever call that function. Then in Update you call Shift, which attempts to access an attribute of that array that doesn't exist.

    did you miss adding configure in Awake/Start?
     
  3. Fuzzytron

    Fuzzytron

    Joined:
    Feb 22, 2018
    Posts:
    13
    Oh my god... that was it haha, thank you very much can't believe I missed that for so long..