Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
4
Question by Ecnalyr · May 25, 2010 at 10:03 PM · scenetimer

Problem when loading a scene for the second time.

My game currently has two scenes.

Scene 1 is the Main Menu, Scene 2 is the level

When my game starts it loads Main Menu, I click to start the game, then it loads the level.

When the level starts, it spawns a bunch of objects, you play the game while its spawning etc. . . . All the while a timer is counting down.

When the timer reaches zero, I have the timer script push me back into the Main Menu using

Application.LoadLevel(0);

The game then returns to the Main Menu.

My problem is this: After the game returns to the Main Menu, if I click to start my level again, the level loads, the timer begins decaying down to zero, but none of my objects respawn (I can't play the actual game).

All that happens the second time the level loads is the timer counts down and then returns me to the main menu.

Where should I look to find what is causing this problem? To me it seems that once the scene is loaded again, it should simply behave the way it did the first time it was loaded. Why wouldn't this be happening?

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

4 Replies

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

Answer by Eric5h5 · May 25, 2010 at 10:25 PM

If you're using any static variables, get rid of them and use regular variables instead. Either that or you have to reset them manually.

Comment
Add comment · Show 9 · 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 Ecnalyr · May 26, 2010 at 12:12 AM 0
Share

I added lines to the script at the end of the timer to reset those static variables to their original values before it returned me to the main menu. It worked :)

avatar image Hakimo · Apr 14, 2011 at 04:46 AM 0
Share

A question for Eric5h5, isn't static vars used for sharing them accross all scripts? If I use regular vars, how do I tell main/game$$anonymous$$anager that an item is already pickep up?

avatar image Eric5h5 · Apr 14, 2011 at 05:24 AM 2
Share

@Hakimo: No, static variables are for when you want static variables. A static variable means there's only one instance per class. If a variable is public, then anything else can access it. http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

avatar image Hakimo · Apr 14, 2011 at 07:14 AM 0
Share

@Eric5h5: Ah I see, thanks again. But it can be shared accross all instances yea? I initially thought that static is used to declare a global variable for Unity. But then again all variable is global unless I declare it inside a function right? Totally misread the 3D platform tutorial. Sorry, one last question as I'm still a bit confused, What are the differences between a static variable and a regular one in terms of behaviours?

avatar image Hakimo · Apr 14, 2011 at 07:45 AM 0
Share

@Eric5h5: Sorry, I forgot to add something. This page says static is used to declare a global variable : http://unity3d.com/support/documentation/ScriptReference/index.$$anonymous$$ember_Variables_26_Global_Variables.html. Does it behave like this only in unity or? Cause my initial understanding of static variables is that they only have one value throughout runtime but after learning unity, it behaves differently...sorry I'm really confused :(

Show more comments
avatar image
2

Answer by hersheys72 · Nov 21, 2016 at 12:39 AM

For me I was using static events, which even though everything that was subscribing was unsubscribing it (and being destroyed) - it was still referencing the first instance of it from initial scene (which no longer existed!!)
Make sure you put void OnDestroy() { EventName = null; }

for every static event in that script.

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 MakMiles · Apr 29 at 09:16 AM 0
Share

Dear hersheys72,

Thank you very much for this comment made in 2016!!! I had exactly this issue and as I faced it for the first time I just didn't understand what is happening. Loading battle screen additively for the first time - everything is working fine. But for the second time - everywhere are errors, that object has been destroyed however everything is on place. I googled but was not able to find any clue. Thanks to your comment I found out that this was exactly events causing the troubles and I was able to fix it. Thank you kind stranger!

avatar image
0

Answer by Tetrad · May 25, 2010 at 11:12 PM

Do you have any objects that have DontDestroyOnLoad called on them?

Comment
Add comment · Show 2 · 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 Ecnalyr · May 25, 2010 at 11:13 PM 0
Share

I do not have any DontDestroyOnLoad objects. I do have one static variable, not quite sure why this would mess things up. I'm going to try to figure out how to 'reset them manually.'

avatar image Eric5h5 · May 25, 2010 at 11:33 PM 1
Share

@Ecnalyr: static variables mess things up because they retain their value always. Reset them by explicitly setting the value in Start or wherever, or better yet don't use them unless you have a good reason for it....

avatar image
0

Answer by Jaakk0S · Jan 29, 2021 at 06:35 AM

I found this old thread healpful and found out I had a static variable issue. The lesson here is that the lifecycle of static variables is the application and not the scene, and this is why static variables in MonoBehaviours need to be initialized in Awake() or Start(). If a static variable is not initialized in one of those places, it will get initialized only on class load (first time the class is referenced during the whole application lifecycle), and if the MonoBehaviour is reloaded (when a new scene is loaded), the variable will retain whatever value it had when the old scene was unloaded as the class is not reloaded.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to load a level after 30 seconds? 3 Answers

Shift from one scene to another (based on time) 1 Answer

Checking the scene/Level you are? 2 Answers

Help with time.time when new scene starts. 1 Answer

How do I enable a script when reaching a place 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