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 a
type' 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