Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MapuHoB · Dec 10, 2015 at 09:10 AM · findobjectsoftype

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!

Comment
Add comment · Show 11
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Addyarb · Dec 10, 2015 at 09:26 AM 0
Share

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);
         }
     }
avatar image MapuHoB · Dec 10, 2015 at 09:32 AM 0
Share

Nope, at least not for now, I am not using the singleton pattern

Opponent$$anonymous$$otionReceiver ScoreAnimation

full code of the classes

avatar image MapuHoB · Dec 10, 2015 at 09:35 AM 0
Share

Strangely for me is that I am also using FindObjectOfType in the Start function in ScoreAnimation and there it works perfectly fine

avatar image Addyarb · Dec 10, 2015 at 10:03 AM 0
Share

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.

avatar image MapuHoB · Dec 10, 2015 at 10:09 AM 0
Share

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");
                 }
         }
     }
avatar image Bonfire-Boy MapuHoB · Dec 10, 2015 at 10:48 AM 0
Share

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.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

From http://gamedev.stackexchange.com/a/112783/59620

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by DAh Right · Dec 10, 2015 at 11:52 PM

Easy ^_^

 var motionReceiver = FindObjectOfType<OpponentMotionReceiver>();

Will return null when:

  1. No one initialized OpponentMotionReceiver component present.

  2. All "Objects who contains" OpponentMotionReceiver component now in state = disabled.

alt text

alt text


1.png (17.3 kB)
2.png (8.4 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lucariolu3d · May 17, 2020 at 08:52 PM 0
Share

thank you so much :D my whole day has been trying to figure this out, and you saved me

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

34 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges