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);
}
GetComponent
functions will returnnull
and/or may produce the error you get. 2°) When callingDestroyObject
, the components will have a flag "I will be destroyed". But you retrieve them when callingGetComponent
resulting in the error. You may add a boolean you set tofalse
in theDestroyObject
function, indicating your sheep can't be clicked anymore. \$\endgroup\$