Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
3
Question by Owen Thomas · Jul 11, 2010 at 01:13 PM · awakefindobjectoftype

FindObjectOfType won't find object that hasn't had "Awake" called?

Hi,

I'm using the following pattern to find a singleton object that I have placed in the scene myself.

public class Program: MonoBehaviour { private static Program s_Instance;

 ...

 public static Program Instance
 {
     get
     {
         if (s_Instance == null)
         {
             UnityEngine.Object program = FindObjectOfType(typeof(Program));
             if (program == null)
                 Debug.LogError("No instance of Program detected.");
             else
             {
                 s_Instance = (Program)program;
             }
         }
         return s_Instance;
     }
 }

 protected override void Awake()
 {
     base.Awake();

     if (!IsValid)
         return;

     if (s_Instance != null && s_Instance != this)
         Debug.LogError("Multiple instances of Program detected.");
     s_Instance = this;

     ...
 }

}

There is another script which references the singleton, and a GameOBject with this script is also present in the scene.

class GameScreen {

 void Awake()
 {
     if (Program.Instance.Game != null)
         OnGameStarted();
     Program.Instance.GameStarted.Register(this);
 }

}

The error I get (sometimes) is:

No instance of Program detected.
UnityEngine.Debug:LogError(Object)
Program:get_Instance() (at Assets\App\Scripts\Program.cs:63)
GameScreen:Awake() (at Assets\App\Scripts\Game\GameScreen.cs:22)

I'm thinking it may be because FindObjectOfType does not consider objects in the scene that have not been "awakened" yet. Is this the case? The documentation of Awake seems to contradict this behaviour:

Awake is called after all objects are initialized so you can safely speak to other objects or query them using eg. GameObject.FindWithTag

Edit: I should add that I am aware that interacting with an object that may not have been "awakened" is a bad idea and I have moved the GameScreen.Awake code into Start. Just curious why it wasn't working as expected.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Tetrad · Jul 11, 2010 at 08:11 PM

The order of Awake is non-deterministic, so right off the bat you shouldn't rely on that. You should change all singleton-accessing startup stuff to Start.

Also, since Awake is called as soon as the object is instantiated, you can save yourself some headache and just set your static instance pointer in awake itself, i.e.

static ClassName instance; public static ClassName Instance { get { return instance; } }

protected override void Awake() { if( instance != null ) { // error condition in the event you want to actually prohibit more than one instance of this object. } instance = this; // etc

}

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

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

No one has followed this question yet.

Related Questions

leaving level 1 Answer

Start() being called more than Once?!? 3 Answers

Awake() is not called automatically by Unity on targets reference 1 Answer

have an enemy change position on the y axis when awake (2d platformer) 0 Answers

How to mock or skip execution of Awake in playmode unit tests? 2 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