- Home /
Object Reference not set to an instance of an Object Help
I keep getting this error and I've had this error through the duration that I've been making my game but I've never really look notice of it since it didn't seem to effect my game. However I believe it's big reason behind massive lag issues within my game but I'm just not sure how to fix it.
This is the error:
and it's point to my Entity Script:
using UnityEngine;
using System.Collections;
public abstract class Entity : MonoBehaviour {
protected Rigidbody2D rig;
protected Animator anim;
protected SpriteRenderer spriteRenderer;
protected ObjectPooler objectPooler;
private float beyondScreenDelay = 5f;
protected virtual void Awake()
{
rig = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
spriteRenderer = GetComponent<SpriteRenderer>();
}
protected virtual void Update()
{
if(!GetComponent<SpriteRenderer>().isVisible)
{
DestroyOffScreen();
}
}
protected void DestroyOffScreen()
{
beyondScreenDelay -= Time.deltaTime;
if(beyondScreenDelay <= 0f)
{
objectPooler.DestroyPooledObject(this.gameObject);
beyondScreenDelay = 5f;
}
}
}
"objectPooler.DestroyPooledObject(this.gameObject);" is simply point to this method in my Object Pooler class:
public void DestroyPooledObject(GameObject obj)
{
obj.SetActive(false);
}
I've got this issue for multiple objects and points to the same method but within different scripts. I've got the same error for my player projectile pointing to this class and method:
public class PlayerBullet : Projectile {
protected override void OnDestroy ()
{
objectPooler.DestroyPooledObject(this.gameObject);
}
}
Can anyone help me fix this error?
Answer by Arkaid · Aug 12, 2016 at 02:04 AM
I doubt it's the reason for your lag problem, but as for why is throwing that error, as far as I can tell from your code, you're not setting "objectPooler" anywhere. Its value remains null throughout the program, and it throws a NullReferenceException when you try to use it.
You need to
objectPooler = _something_;
before you can use it
Your answer
Follow this Question
Related Questions
Can't Get animation.Play to work 0 Answers
Unity was not able to run my Invoke command, and im not sure why. 1 Answer
IndexOutOfRangeException help on debug 3 Answers
Using entities and jobs 1 Answer