Hi,
I’m working on a simple 3d shooter that has pillars that disappear if the player tries to hide behind them for too long.
To do this, I detect the presence of the player next to the pillar and if it exceeds a certain amount of seconds, the pilar fades out. Once the player moves away, the pillar returns.
Because my player has many different collisions to detect, I use one script to control all collisions and triggers. Once the collision happens, and time has passed, an event is sent and pillar fades:
private void OnCollisionStay(Collision collision)
{
if (collision.gameObject.tag == "AlertTouch")
{
if (collision.gameObject.transform.parent.gameObject.tag == "Pillar")
{
GameObject pillar = collision.gameObject.transform.parent.gameObject;
Pillar pillarScript = pillar.GetComponent<Pillar>();
alertTouchTime += Time.deltaTime;
if (alertTouchTime >= pillarScript.disappearTime)
{
alertTouchTime = 0;
OnPillarFade.Invoke(0, false, pillar);
}
}
}
}
This worked great until recently when I started getting error messages:
“MissingReferenceException: The object of type ‘Pillar’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.”
type Pillar is the script attached to the pillar that has the listener for the event:
private void Update()
{
Debug.Log("From update: " + this);
}
void HandleFade(float fadeValue, bool colliderEnabled, GameObject pillar)
{
Debug.Log("From fade " + this);
if (pillar == this.gameObject)
{
myRenderer.material.DOFade(fadeValue, fadePerSecond);
myCollider.enabled = colliderEnabled;
}
}
I check (pillar == this.gameObject) because there are multiple pillars that all listen to the collision and I need to make sure this is the pillar in question.
From the two separate Debug.log calls I get:
“From update: Pillar(Clone) (Pillar)
UnityEngine.Debug:Log(Object)
Pillar:Update() (at Assets/Scripts/Obstacles/Pillar.cs:24)”
AND
“From fade: null
UnityEngine.Debug:Log(Object)
Pillar:HandleFade(Single, Boolean, GameObject) (at Assets/Scripts/Obstacles/Pillar.cs:29)
PlayerCollisionDetection:OnCollisionStay(Collision) (at Assets/Scripts/Utilities/PlayerCollisionDetection.cs:40)”
at the same time. How can this even happen? I’m not destroying the pillar at any point, not enable/disable anything.
I would appreciate any help, and thanks in advance