Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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
2
Question by FreeTimeDev · Oct 27, 2011 at 05:54 PM · levelpauserestartapplication.loadlevel

Application.LoadLevel not Resetting

I have a simple button that's intended to restart the level.

The player starts in the level "Arcade" and when dies, goes to "LocalHighScore" where it shows the scoreboard and he is given two options: quit or Again!.

The button for again is thus:

 if (GUI.Button(Rect(Screen.width *.7,Screen.height * .6, 128,64),"Again !"))
 {
 Application.LoadLevel("Arcade");
 }

It loads the level, but nothing is reset. If I had to manually reset every variable to it's initial setting I'd never stop forgetting one or two. According to the reference everything is scrapped and rebuilt unless you use DontDestroyOnLoad--and I never have. For anything. I'm a little confused as to why this happens.

(The actual effect is after reloading the level the player can't move, the timer stops, their health is negative, nothing spawns. Opening the menu is broken)

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

Answer by Bunny83 · Oct 27, 2011 at 06:56 PM

I guess you use static variables? Now you know why you should avoid static variables because they don't belong to any object instance and therefore will never be reset. Work with objects and references instead of static stuff. The only other way is to (re)set them manually in a function.

If you don't use static variables (but i guess you do) i have no idea what's going wrong.

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 FreeTimeDev · Oct 27, 2011 at 08:02 PM 0
Share

Ugh, that's terrible. I'm probably going to have to rewrite a ton of my code for that. No one through that warning out there before.

avatar image Kmulla · Apr 19, 2012 at 02:39 PM 1
Share

When using static variables you just have to assign the static variable value inside the Start() or Awake() function.

Link for more explanation: http://www.41post.com/3424/program$$anonymous$$g/using-static-variables-in-unity3d

avatar image thekingofclubs · Sep 03, 2014 at 09:30 PM 0
Share

This might be a silly question, but is using objects and references more slow/error prone than using static variables?

avatar image
2

Answer by warddav16 · Jun 06, 2012 at 07:32 PM

A good work around is to use a singleton pattern instead of static variables.

myClass : MonoBehaviour {

 private static myClass mInstance;
 public static myClass Instance() {return mInstance;}

 void Awake() 
 {
     if(!mInstance) mInstance = this;
 }

 //All you variables go here. This will ensure there is only one type of an object at a 
 //time, but will still destroy and clear out your variables in every scene
 //To access the variables, in your other scripts you will say 
 //myClass.Instance().myPublicVariableOrFunction

}

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

Restart Problem 1 Answer

Level Not Loading After Screen Fade 1 Answer

Can't go back to the game scene from main menu for the second time 2 Answers

How to load (restart) last scene 2 Answers

Resetting a Scene 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