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 ronronmx · Apr 11, 2011 at 06:28 AM · loadleveldontdestroyonload

Method execution order after LoadLevel()?

I'm a little confused about the order of execution when using "LoadLevel()" with an class using "DontDestroyOnLoad()".

I know that Awake() is always before OnEnable() or Start(), but it seems that I have to use "OnEnable()" after a level load to access objects in the scene because it won't find them in Awake().

Again, I only have this problem when using DontDestroyOnLoad().

Any help will be greatly appreciated! Stephane

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
1
Best Answer

Answer by Ariel J · Apr 11, 2011 at 06:42 AM

You probably want to try using start, as OnEnable is called when the object becomes active.

Here is the order:

Awake is called - this is called as soon as the object is in the scene Start is called - this is called when the object becomes active Update and FixedUpdate are called from then on

hope this helps!

-Aj

Comment
Add comment · Show 5 · 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 ronronmx · Apr 11, 2011 at 06:56 PM 0
Share

Thanks for your help SuperCheese. So if I understand correctly, Awake() is the first function called after using LoadLevel()? Or is Awake() called before LoadLevel()? If Awake() is called before the level loads, then it makes sense that I can't find my objects when looking them up in Awake(), as they don't exist yet.

avatar image Ariel J · Apr 12, 2011 at 12:20 AM 0
Share

I'm pretty sure that awake will have been only called in the first scene the object was in. So yes, in this case I reckon that awake would have been called before the LoadLevel(). Here is a couple of examples:

You have an object already placed in the scene. When you hit play, awake is called first. Awake is called when the object/script is being loaded. When you load a new scene, it simply doesn't destroy the object.

You instantiate an object at runtime. Awake is called when it loads. This is the same as before, but it is completed during the game.

avatar image Ariel J · Apr 12, 2011 at 12:41 AM 0
Share

After some research, I've found what you may want to use.

Here is a script I made:

var found : GameObject;

function Start () { //Find the initial object(s) and make sure it doesn't get destroyed found = GameObject.FindWithTag ("GameController"); DontDestroyOnLoad (transform.gameObject); }

function OnLevelWasLoaded () { //If a level was loaded, we look for them again! found = GameObject.FindWithTag ("GameController"); }

So sorry for the confusion! The above example should fix your problems!

-AJ

avatar image ronronmx · Apr 13, 2011 at 05:33 AM 0
Share

SuperCheese thanks a lot for your help! You are right about Awake() and Start() not being called again in a class using DontDestroyOnLoad(). Since I was doing all my caching either in Awake() or Start(), it never got ran again after a level load. By adding the same code to OnLevelWasLoaded(), it works like intended.

Thanks again!

avatar image Ariel J · Apr 13, 2011 at 09:23 AM 0
Share

No probs :D Good luck in Unity!

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

DontDestroyOnLoad does not work after reload the scene? 1 Answer

Scene changing 1 Answer

combine words of different scenes in a list or array; have each word link to a page 3 Answers

Nav Mesh SetDestination can't be called 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