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
0
Question by Johnnemann · Aug 01, 2012 at 02:26 AM · referencestartawake

Object reference becomes null between Awake and Start after scene load

I have an odd problem. I'm using the same pattern throughout a bunch of components - in Awake() I find a reference to an object in the scene, and then in Start() I call a function on that object.

For one of my components (attached to my player object), the reference to the object becomes null between the Awake() and Start() calls. The variable is private and there's no function within the class changing its value, so short of Unity messing with things behind the scenes or actual memory corruption I'm not sure how this is possible. Anyone have any ideas?

The object in question (the saver) is DontDestroyOnLoad, and this happens only after loading a new scene.

 private SaveManager saver;
 void Awake()
 {
     GameObject SManager = GameObject.Find("SaveManager");
     if (SManager)
     {
         saver = SManager.GetComponent<SaveManager>();
         if (!saver)
         {
             Debug.LogError("No save manager in the scene!");
         }
     }
      //"saver" is a valid reference here.
 }

 void Start()
 {
      //"saver" is null here
      //If I Find() it again, it's a valid reference.
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Kryptos · Aug 01, 2012 at 10:16 AM

Is this script also on a DontDestroyOnLoad object? If it is the case, Awake is not called again. And the solution would be to do the "Find" in the OnLevelWasLoaded method.

Comment
Add comment · Show 3 · 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 Johnnemann · Aug 01, 2012 at 04:45 PM 0
Share

Nope. Both Awake() and Start() are called, in the proper order (I use Debug.Log() to output the value of saver in each).

avatar image Kryptos · Aug 02, 2012 at 07:55 AM 0
Share

Since I use the same kind of code in all my project, I don't understand it. Are you sure that nothing else can modify this value?

avatar image Johnnemann · Aug 02, 2012 at 07:35 PM 0
Share

None of my code modifies it. It's possible something in Unity does, though. Or GC, for some reason? I have other objects using this same pattern, and none exhibit the issue, so it's something about this component/object.

avatar image
0

Answer by Dzxyan · Oct 14, 2016 at 11:18 AM

Hi I know this is old topic,but I also have this problem, do you have solution for that?

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

8 People are following this question.

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

Related Questions

How to reference GameObject in Awake or Start 2 Answers

Why do I get a context error when using GameObject.FindWithTag in Awake/Start function and GetComponent in a function? 1 Answer

Is there a 'Play On Awake' function for animations? 1 Answer

Strange Race condition for one prefab 1 Answer

Start() being called more than Once?!? 3 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