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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by MarvinH · Nov 09, 2012 at 06:52 PM · gamevariablesgamecontroller

How do I Reset My Variables On GameOver?

Hello,

I am trying to reset my variables when the player loses or wins, and he hits 'new game' Right now, just to test the code, I created a function called 'reset' which resets three variables (lives, bombs, score) to zero (for the purposes of testing). I put this code in the gamecontroller object. Which I placed in my menu scene.

I call this function when the player hits 'new game' on the menu screen. I also have a gameController object in the level that I load. I also update the GUI via script. The Reset function doesn't seem to work. Instead, it's pulling the variables directly from the GmeController script's Awake function, and not resetting them.

What am I doing wrong? Should I only have one game controller throughout my entire game? Is calling Reset before a Scene Load function the wrong way to go about it?

Game Controller Script

 static var GameOver : boolean;
 var Player : GameObject; 
 
 //set the player score 
 var score : int;
 var lives : int;
 var bombs : int;  
 
 
 //GUI Variables:
 var scoreText : UILabel;
 var livesText : UILabel; 
 var bombText : UILabel;  
 var gameController:GameController; 
 //var PowerUpSound: GameObject;      
 
 
 
 function Awake(){
 
 lives= 1; 
 bombs = 1;  
 GameOver = false;
 
 
 totalEnemies = 1 ; 
 isWin  = false;
 crabturret = 1;
 DontDestroyOnLoad(this);
 UpdateHUD();
 
 }
 
 
 
 
 
 function Update(){
     //print(lives);
     
 }
 
 
 
 function Respawn(){
     
     yield WaitForSeconds(.1);
     Instantiate(Player, Vector3(-18.0759, 0, -1.6689), Quaternion.Euler(-90, -180,0));
 }
 
 
 // Ui stuff 
 function UpdateHUD()
 {
     livesText.text = "Lives: " + lives; 
     scoreText.text = "Score: "+ score;
     bombText.text = "Bombs: "+ bombs;
 }
 
 
 //strt the game over 
 function Reset(){
 
 lives= 0; 
 bombs = 0;  
 score = 100; 
 GameOver = false;
 
 }
 

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
0

Answer by homer_3 · Nov 09, 2012 at 09:51 PM

If you are reloading the scene, your object you called reset on will get destroyed and a new one will be created, which will then call its awake method.

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 MarvinH · Nov 09, 2012 at 09:54 PM 0
Share

Thanks,

I think I used a Donot destroy on load command. And I placed the reset script inside the Gamecontroller script/object. So it's ok to have two GameControllers? One in each scene?

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

getting udp package info inside unity (GlovePIE) 0 Answers

Help with scriptableobjects 1 Answer

join two scripts to use one var in both scripts 1 Answer

How to make the level screen????? 0 Answers

Error BCE0077 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