0
\$\begingroup\$

I made a small method that will quickly give the player a "flashlike" display when a sheep is killed. However clicking too fast will trigger a missingReferenceException. Because it registers the raycast a second time. Is this due to the delay on the DestroyObject() or something else?

private IEnumerator startFlashing(GameObject sheep)
{  
    sheep.gameObject.GetComponent<Rigidbody>().freezeRotation = true;
    sheep.gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
    sheep.SetActive(false);
    yield return new WaitForSeconds(theTimeBetweenFlashes);
    sheep.SetActive(true);
    DestroyObject(sheep.gameObject, 0.1f);    
}
\$\endgroup\$
3
  • 2
    \$\begingroup\$ I see two potential reasons : 1°) When disabling a gameobject, the GetComponent functions will return null and/or may produce the error you get. 2°) When calling DestroyObject, the components will have a flag "I will be destroyed". But you retrieve them when calling GetComponent resulting in the error. You may add a boolean you set to false in the DestroyObject function, indicating your sheep can't be clicked anymore. \$\endgroup\$
    – Hellium
    Commented Oct 17, 2017 at 13:10
  • \$\begingroup\$ @Hellium Ye I noticed that it is due to the 2nd iteration incase it gets clicked twice, while "active" . This result in it trying to continue on with data not there. A boolean for this should fix it. Thanks! \$\endgroup\$ Commented Oct 17, 2017 at 18:49
  • 1
    \$\begingroup\$ @Hellium that looks fleshed-out enough to serve as an answer that could be Accepted. :) \$\endgroup\$
    – DMGregory
    Commented Oct 18, 2017 at 2:43

1 Answer 1

1
\$\begingroup\$

With the support of @DMGregory, I will post my comment as an answer :

When calling DestroyObject (which surely calls Destroy), the sheep object and its components (rigidbody) will turn to true a flag saying "I will be destroyed".

When you retrieve the Rigidbody using GetComponent, the function checks this flags and warns you with the message.

I advise you to define a boolean you set to false in the DestroyObject function indicating your sheep can't be clicked anymore :

private bool destroyInProgress = false ;

private IEnumerator startFlashing(GameObject sheep)
{
    if( destroyInProgress ) yield break ;

    sheep.gameObject.GetComponent<Rigidbody>().freezeRotation = true;
    sheep.gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
    sheep.SetActive(false);
    yield return new WaitForSeconds(theTimeBetweenFlashes);
    sheep.SetActive(true);
    DestroyObject(sheep.gameObject, 0.1f);
    destroyInProgress = true ;
}
\$\endgroup\$
2
  • \$\begingroup\$ Though it doesn't crash the game any longer I still receiver the Exception. Is something to be ignored or do you recommend a different approach? \$\endgroup\$ Commented Oct 18, 2017 at 10:51
  • \$\begingroup\$ What's the full error? Does it concern the Rigidbody component or the gameobject? Does the error point to the startFlashing function or an other place in the code? \$\endgroup\$
    – Hellium
    Commented Oct 18, 2017 at 11:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .