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 /
This question was closed Jan 06, 2014 at 03:32 AM by Next Beat Games for the following reason:

This Question is invalid - user error

avatar image
0
Question by Next Beat Games · Jan 05, 2014 at 07:22 PM · startawakeexecution ordernull reference

Cached references set in awake aren't persisting

Hi. I have an interesting bug probably resulting in an over sighted "gotcha".

I have a script "A" with some variables

 Material myMaterial; // private material reference
 public Renderer myRenderer; // public renderer reference assigned in inspector

Its awake function:

 void Awake()
 {
     myMaterial = myRenderer.material;
     Debug.Log(myMaterial); // this is not null
 }

It has another function called DoStuff

 public void DoStuff()
 {
     Debug.Log(myMaterial); // this shows as null (details below)
     myMaterial.color = new Color(blablabla);
 }

Then I have another script "B" that accesses DoStuff in its Start function

 void Start()
 {
     aScript.DoStuff(); // I get an error that myMaterial is not set in "A"
 }


more details: i have 2 scenes that make use of these two scripts.. in one scene i get no error but i get the error in the other scene because the script execution order seems to be different.

but script order execution shouldn't matter because all Awake() functions are called before any Start() functions.

So the order in the scene where there is the reference error is as follows:

 A.Awake()
 B.Awake()
 A.Start()
 B.Start()

in the scene where there is no bug uses the following order

 B.Awake()
 A.Awake()
 B.Start()
 A.Start()
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

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Awake called after I activate object, not after Instantiate... is it normal?? 2 Answers

Awake/Start order across different platforms. 3 Answers

ExecuteInEditMode Component construct "twice" and "reset" custom classes 0 Answers

Execution Order of Scripts - Awake and OnEnable 1 Answer

Timer that stops at the end of the game 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