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
2
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
Add comment · 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
5
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
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 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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Should I create local copy of global variable? 3 Answers

Best way to share a variable 1 Answer

Most convenient and fast way to persist/access/modify many values from scene to scene? (C#) 0 Answers

Error Assigning GameObject To Var Inside A Class 3 Answers

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


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