0
\$\begingroup\$

Why would I get the following log?

  • Start OpponentMotionReceiver
  • motion receiver not found
  • true

    public class ScoreAnimation : MonoBehaviour
    {
        private OpponentMotionReceiver cachedObject;

        private void Start()
        {
            cachedObject = FindObjectOfType<OpponentMotionReceiver>();
        }

        private void OnDestroy()
        {
                var motionReceiver = FindObjectOfType<OpponentMotionReceiver>();

                if (motionReceiver == null)
                {
                    Debug.Log("motion receiver not found");
                }

                if(cachedObject != null)
                {
                    //prints true, another proof that the gameObject is active
                    Debug.Log(cachedObject.gameObject.activeInHierarchy);
                }
        }
    }

public class OpponentMotionReceiver : MonoBehaviour
    {
        private void Start()
        {
            Debug.Log("Start OpponentMotionReceiver");
        }

        private void OnDisable()
        {
            //never enters
            Debug.Log("OnDisable OpponentMotionReceiver");
        }

        private void OnDestroy()
        {
            //never enters
            Debug.Log("OnDestroy OpponentMotionReceiver");
        }
    }

P.S. This is extremely simplified version of the code so that the rest brings no confusion. If you need more details, I'd be pleased to answer you!

\$\endgroup\$
1
  • \$\begingroup\$ What do u actually want to do? Is that a GameObject? and do u want to find that? \$\endgroup\$ Dec 10, 2015 at 10:52

1 Answer 1

2
\$\begingroup\$

Hopefully you are changing the scene right? Only at that time unity behaves like that.

\$\endgroup\$
3
  • \$\begingroup\$ yeah I am changing the scene but do you mean that FindObjectOfType doesn't work while loading another scene? Because I am more than confident that the OpponentMotionReceiver object neither gets deactivated nor destroyed. \$\endgroup\$
    – ZenVentzi
    Dec 10, 2015 at 20:02
  • \$\begingroup\$ exactly, I meant the same. My suggestion would be to keep that instance before changing the scene for use. \$\endgroup\$ Dec 10, 2015 at 20:09
  • \$\begingroup\$ WOW :D literally tried some of the dumbest things but never expected FindObjectOfType not to work in case of loading a new scene, HAH :D So it appears that no matter if a gameObject is being active or inactive or destroyed, just FindObjectOfType doesn't work on loading a new scene, funny :D Thanks a lot :D \$\endgroup\$
    – ZenVentzi
    Dec 10, 2015 at 20:16

You must log in to answer this question.

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