- Home /
Why is FindObjectOfType(MyType) not finding anything?
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!
Are you using Opponent$$anonymous$$otionReceiver as a public static class / singleton? If so, you need to add something like this in the Awake function on that class:
public static Opponent$$anonymous$$otionReciever current;
void Awake()
{
$$anonymous$$akeThisOnlyInstance();
}
void $$anonymous$$akeThisOnlyInstance()
{
if (current == null) {
DontDestroyOnLoad(gameObject);
current = this;
} else if (current != this) {
Debug.Log(current);
//Destroy(gameObject);
}
}
Nope, at least not for now, I am not using the singleton pattern
Opponent$$anonymous$$otionReceiver ScoreAnimation
full code of the classes
Strangely for me is that I am also using FindObjectOfType in the Start function in ScoreAnimation and there it works perfectly fine
Can you verify that said Opponent$$anonymous$$otionReceiver is not being destroyed somehow? This is my first thought. If it doesn't exist in the scene, you'll need to figure out what is destroying it or why in order to resolve this. I doubt that it is the FindObjectOfType method that is causing the issue.
Other than check if OnDestroy is called I checked by caching the object in the Start function in ScoreAnimation, here is the code:
//the log that I got here was: motion receiver not found
public class ScoreAnimation : $$anonymous$$onoBehaviour
{
private Opponent$$anonymous$$otionReceiver opponent$$anonymous$$otionReceiver;
private void Start()
{
opponent$$anonymous$$otionReceiver = FindObjectOfType<Opponent$$anonymous$$otionReceiver>();
}
[UsedImplicitly]
private void OnDestroy()
{
var motionReceiver = FindObjectOfType<Opponent$$anonymous$$otionReceiver>();
if (motionReceiver == null)
{
Debug.Log("motion receiver not found");
}
if (opponent$$anonymous$$otionReceiver == null)
{
Debug.Log("cached object is null");
}
}
}
So are you saying that the version with the cached object works ok (ie the "cached object" isn't null and you can do stuff with it in ScoreAnimation.$$anonymous$$onoBehaviour)?
It feels a bit strange to me to be searching for an object like this in OnDestroy. If the object is being destroyed because of scene ending then you can't predict what state other objects are going to be in at the time the function is run.
Answer by MapuHoB · Dec 14, 2015 at 07:28 AM
Changing the scene. Only at that time unity behaves like that. No matter if the object we are looking for is active or not, simply FindObjectOfType doesn't work at this point of the game cycle.
Answer by DAh Right · Dec 10, 2015 at 11:52 PM
Easy ^_^
var motionReceiver = FindObjectOfType<OpponentMotionReceiver>();
Will return null when:
No one initialized OpponentMotionReceiver component present.
All "Objects who contains" OpponentMotionReceiver component now in state = disabled.
thank you so much :D my whole day has been trying to figure this out, and you saved me
Your answer
![](https://koobas.hobune.stream/wayback/20220612052046im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
FindObjectsOfType not find all objects and code not do anything 0 Answers
Where is ScriptableObject.CreateInstance stored? 2 Answers
FindObjectsOfType for abstract generic class 0 Answers
Find all components of specific type, as fast as possible 0 Answers
FindObjectsOfType doesn't find instantiated objects 0 Answers