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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Parallaxer : 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; //how fast the object is moving
public float spawnRate; //how often it spawns
public YSpawnRange ySpawnRange;
public Vector3 defaultSpawnPos; //its starting position
public bool spawnImmediate; //whether it will spawn immediately
public Vector3 immediateSpawnPos; //the immediate spawn position
public Vector2 targetAspectRatio; //target aspect ratio, accessibility stuff
float spawnTimer;
float targetAspect;
PoolObject[] poolObjects;
GameManager game;
void Awake() //for initialisation
{
}
void Start() //initialisate game
{
game = GameManager.Instance;
}
void OnEnable()
{
GameManager.GameOverConfirmed += GameOverConfirmed;
}
void OnDisable()
{
GameManager.GameOverConfirmed -= GameOverConfirmed;
}
void GameOverConfirmed()
{
for(int i = 0; i < poolObjects.Length; i++)
{
poolObjects[i].Dispose();
poolObjects[i].transform.position = Vector3.one * 1000; //off screen
}
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); //has the parent of object script is linked to
t.position = Vector3.one * 1000; //offscreen again to avoid user seeing it
poolObjects[i] = new PoolObject(t);
}
}
void Spawn()
{
Transform t = GetPoolObject();
if (t == null) return; //true means pool size is too small
Vector3 pos = Vector3.zero;
pos.x = defaultSpawnPos.x;
pos.y = Random.Range(ySpawnRange.min, ySpawnRange.max);
t.position = pos;
}
void SpawnImmediate()//two objects to avoid gaps between objects
{
Transform t = GetPoolObject();
if (t == null) return; //true means pool size 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++)
{
poolObjects[i].transform.localPosition += -Vector3.right * shiftSpeed * Time.deltaTime;
CheckDisposeObject(poolObjects[i]); //checking if the position is less than spawn position
}
}
void CheckDisposeObject(PoolObject poolObject)
{
if (poolObject.transform.position.x < -defaultSpawnPos.x) //should spawn offscreen so assume negative x is out of sight so can be removed
{
poolObject.Dispose();
poolObject.transform.position = Vector3.one * 1000; //high value the user wont see
}
}
Transform GetPoolObject()
{
for (int i = 0; i < poolObjects.Length; i++)
{
if (!poolObjects[i].inUse)
{
poolObjects[i].Use();
return poolObjects[i].transform;
}
}
return null;
}
}
Here are my inspector screens etc:


Anyone got any ideas?