0

Hi guys i've been struggling for 3 days now trying to fix this error in my code ; NullReferenceException: Object reference not set to an instance of an object Parallaxerr.Shift () (at Assets/scripts/Parallaxerr.cs:134) Parallaxerr.Update () (at Assets/scripts/Parallaxerr.cs:82)

This is my code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class Parallaxerr : MonoBehaviour
{

    class PoolObject
    {
        public Transform transform;
        public bool inUse;
        public PoolObject(Transform t) { transform = t; }
        public void Use() { inUse = true; }
        public void Dispose() { inUse = false; }

    }

    [System.Serializable]
    public struct YSpawnRange
    {
        public float min;
        public float max;
    }

    public GameObject Prefab;
    public int poolSize;
    public float shiftSpeed;
    public float spawnRate;


    public YSpawnRange ySpawnRange;
    public Vector3 defaultSpawnPos;
    public bool spawnImmediate;//particle prewarm
    public Vector3 immediateSpawnPos;
    public Vector2 targetAspectRatio;

    float spawnTimer;
    float targetAspect;
    PoolObject[] poolObjects; //step 1 ; variable declared 
    GameManager game;

    void Awake()
    {

    }

    void Start()
    {
        game = GameManager.Instance;
    }

    void OnEnable()
    {
        GameManager.OnGameOverConfirmed += OnGameOverConfirmed;
    }

    void OnDisable()
    {
        GameManager.OnGameOverConfirmed += OnGameOverConfirmed;
    }

    void OnGameOverConfirmed()
    {
        for (int i = 0; i < poolObjects.Length; i++)
        {
            poolObjects[i].Dispose();
            poolObjects[i].transform.position = Vector3.one * 1000;
        }

        if (spawnImmediate)
        {
            SpawnImmediate();
        }
    }


    void Update()
    {
        if (game.GameOver) return;

        Shift();
        spawnTimer += Time.deltaTime;
        if (spawnTimer > spawnRate)
        {
            Spawn();
            spawnTimer = 0;
        }
    }

    void Configure()
    {
        targetAspect = targetAspectRatio.x / targetAspectRatio.y;
        poolObjects = new PoolObject[poolSize];
        for (int i = 0; i < poolObjects.Length; i++)
        {
            GameObject go = Instantiate(Prefab) as GameObject;
            Transform t = go.transform;
            t.SetParent(transform);
            t.position = Vector3.one * 1000;
            poolObjects[i] = new PoolObject(t);
        }

        if (spawnImmediate)
        {
            SpawnImmediate();
        }
    }

    void Spawn()
    {
        Transform t = GetPoolObject();
        if (t == null) return;//if true , this indicates that poolSize is too small
        Vector3 pos = Vector3.zero;
        pos.x = defaultSpawnPos.x;
        pos.y = Random.Range(ySpawnRange.min, ySpawnRange.max);
        t.position = pos;
    }

    void SpawnImmediate()
    {
        Transform t = GetPoolObject();
        if (t == null) return;//if true , this indicates that poolSize is too small
        Vector3 pos = Vector3.zero;
        pos.x = immediateSpawnPos.x;
        pos.y = Random.Range(ySpawnRange.min, ySpawnRange.max);
        t.position = pos;
        Spawn();
    }


    void Shift()
    {
        for (int i = 0; i < poolObjects.Length; i++) //step 3 ; variable used
        {
            poolObjects[i].transform.position += -Vector3.right * shiftSpeed * Time.deltaTime;
            CheckDisposeObject(poolObjects[i]);
        }
    }

    void CheckDisposeObject(PoolObject poolObject)
    {
        if (poolObject.transform.position.x < -defaultSpawnPos.x)
        {
            poolObject.Dispose();
            poolObject.transform.position = Vector3.one * 1000;
        }
    }

    Transform GetPoolObject()
    {
        for (int i = 0; i < poolObjects.Length; i++)
        {
            if (!poolObjects[i].inUse)
            {
                poolObjects[i].Use();
                return poolObjects[i].transform;
            }
        }
        return null;
    }
}

I understand that the PoolObject is declared but it is not outputting any value or is outputting null. I tried to assign a value to the PoolObject by using this method : PoolObject = GetComponent (); but i then get another error that reads ; Assets/scripts/Parallaxerr.cs(45,9): error CS0118: Parallaxerr.PoolObject' is atype' but a `variable' was expected and Assets/scripts/Parallaxerr.cs(45,35): error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer

Can somebody please show me a way to assign a value to the PoolObject so that my code can work please

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
yogi
  • 1
  • 1
  • 3
    Which line of code gives the exception? – Chetan Jul 26 '18 at 10:40
  • line 134 and 82 – yogi Jul 26 '18 at 10:42
  • 1
    In line 145/146, you fist dispose the object and then change values on it? Doesn't sound right. Same in line 67/68. – Thomas Weller Jul 26 '18 at 10:44
  • And how would I know which is line134 in your code? – Chetan Jul 26 '18 at 10:45
  • @ChetanRanpariya my bad , new to the forum, – yogi Jul 26 '18 at 10:48
  • for (int i = 0; i < poolObjects.Length; i++) //step 3 ; variable used – yogi Jul 26 '18 at 10:48
  • void Update() { if (game.GameOver) return; Shift(); spawnTimer += Time.deltaTime; if (spawnTimer > spawnRate) { Spawn(); spawnTimer = 0; } } – yogi Jul 26 '18 at 10:50
  • Are you calling configure method before calling shift method? – Chetan Jul 26 '18 at 10:52
  • @ChetanRanpariya yes in my code i'm calling the configure method before the shift method. – yogi Jul 26 '18 at 11:36
  • @ThomasWeller yes I do but from my understanding that has nothing to do with Object reference not set to an instance of an object. However if i'm mistaken please forgive my ignorance as I only started learning C at the beginning of this month – yogi Jul 26 '18 at 11:42
  • Are you calling configure method before calling Update method? Can you share the code which uses this `Parallaxerr` class object and calls methods on it? – Chetan Jul 26 '18 at 12:06
  • @ChetanRanpariya No I am calling the Update method first and then the configure method. – yogi Jul 26 '18 at 12:11
  • @ChetanRanpariya the code is too long to add to the comments , could i maybe email it to you? – yogi Jul 26 '18 at 12:14
  • Configure method initializes the `poolObjects` array. And shift method uses this the array. So if you are calling Update method first that means shift method tries to use uninitialized array which is null. So you need to fix the code to call configure method first before calling any other methods. – Chetan Jul 26 '18 at 12:16
  • @ChetanRanpariya I just did that and called the configure method first but there is no difference i am still getting the NullReferenceException on the same code – yogi Jul 26 '18 at 12:38

0 Answers0