- Home /
Paralax not working
Im experiencing a run time error called:
NullReferenceException: Object reference not set to an instance of an object Parallaxer.Shift () (at Assets/Scripts/Parallaxer.cs:128)
I've been checking the code for a few minutes now and I can't seem to find the answer. I know that it might have something to do with me not calling me objects properly. Please help me out.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Parallaxer : MonoBehaviour
{
class PoolObject //responsible for pooling objects together. Determines wheter objects are in use
{
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 Prefabs;
public int poolSize; //determines number of objects to spawn
public float Shiftspeed; // How fast objects move
public float spawnRate; // determines how often objects spawn
public YspawnRange yspawnRange;
public Vector3 defaultSpawnPos;
public bool spawnImmediate; //determines whether objects spawn on start up, particel prewarm
public Vector3 immediateSpawnPos;
public Vector2 targetAspectRatio;
float spawnTimer; //timer for spawn
float targetAspect;
GameManager game;
PoolObject[] poolObjects;
void Awake()
{
}
// Start is called before the first frame update
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();
}
}
// Update is called once per frame
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(Prefabs) 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; //Indicates poolsize is 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; //Indicates poolsize is small;
Vector3 pos = Vector3.zero;
pos.x = defaultSpawnPos.x;
pos.y = Random.Range(yspawnRange.min, yspawnRange.max);
t.position = pos;
Spawn();
}
void Shift()
{
for (int i=0; i<poolObjects.Length; i++)
{
poolObjects[i].transform.localPosition += -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;
}
}
Answer by jstopyraIGG · Mar 28, 2019 at 06:00 PM
Do you ever call Configure()? It seems like your array of poolObjects[] is not existent, it seems like you populate it in Configure() function, dont forget to call that before you call Shift()!
Oh my I've been focus on Shift() and configure() for too long, that I didn't noticed that I haven't placed the configure() function on awake(). THank you so much!