• 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
Question by BarkShark · Mar 22, 2011 at 09:11 PM · staticscenesvar

Problem with static variables

Hi

I'm creating a game with different levels. I have a Main Menu and also a level selection menu. I also have 2 scripts. I'd like to know how it is possible to only select the levels in the level selection menu that you already have finished. I've tried it with static variables but now I know that static variables reset if you switch a scene.

This is my Move Around Script:

var speed = 6.0; var JumpSpeed = 8.0; var gravity = 20.0; private var dead = false; private var moveDirection = Vector3.zero; private var grounded : boolean = false; var myClip : AudioClip; static var Level1Enabled = true; static var Level2Enabled = false;

function OnControllerColliderHit(hit: ControllerColliderHit) // Fall To Dead { if(hit.gameObject.tag =="Fallout") { dead = true; HealthControl.LIVES -=1; } //End Level if(hit.gameObject.tag =="End1") { Level2Enabled = true; Application.LoadLevel(3); // selection menu

} }

Script atached to the gui.text in the selection menu for level 2.

var level2Finished = MoveAround.Level2Enabled; function OnMouseEnter() { if(level2Finished) { renderer.material.color = Color.green; } } function OnMouseExit() { renderer.material.color = Color.white; }

function OnMouseUp() { if(level2Finished) { Application.LoadLevel(4); } }

Comment
Uriel_96
Statement

People who like this

2 Show 1
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 Peter G · Mar 22, 2011 at 10:10 PM 1
Share

static vars shouldn't reset when you load the scene.

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by karl_ · Mar 22, 2011 at 11:19 PM

If you're going to allow players to exit and resume the game, I'd look into the PlayerPrefs class ( http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html ). This will allow you to save variables through different playthroughs. So for example, if you finish level 1 do something like :

function onLevelComplete()
{
    PlayerPrefs.SetInt("levelsComplete", 1);
}

So for this example levelsComplete is an int representing the amount of levels complete. Your menu script would check something like this:

if(PlayerPrefs.GetInt("levelsComplete") >= 1) 
//    makeLevelOneClickable
Comment
Statement
Kristov
Joshua
_Petroz
BarkShark

People who like this

5 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 Joshua · Mar 23, 2011 at 12:42 AM 0
Share

Good answer. Also, if you have a problem with things reseting when changing a scene, use DontDestroyOnLoad.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Instantiating a Coroutine? 2 Answers

Error Assigning GameObject To Var Inside A Class 3 Answers

Working between different scenes! 1 Answer

Accessing a js static var from a c# script 1 Answer

Make Score Go Up At a Collision 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