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
0
Question by MakeITeasy · Dec 10, 2013 at 07:39 PM · javascriptscoreguitextpointsinitialization

How to set points back to zero when reload the scene?

hello to all!

I'm making a memory card game. i flip two card objects in my game and when i click on them both (which they have the same image) i get 10points and they both get destroyed. I have a js script ScoreManager that controls the points. Also there is a timer counts and if it becomes equal to zero then a game over GUITexture appears and loads back the main menu.

This is the js script for card 1 (there is another one js script for gameobject card 2):

     function OnMouseDown () {
     card_1.transform.localRotation.z = 180;
     flipedCard = true;
     Invoke("CheckCards", 1);
     }
     function CheckCards(){
     if((flipedCard == true) && (Flip_card_2.flipedCard == true)){
     flag=true;
     Destroy(card_1);
     }
     else if(flipedCard == true){
     card_1.transform.localRotation.z =0;
     flipedCard=false;
     }
     }
     //variables
     public var card_1: GameObject;
     card_1 = GameObject.Find("card_1");
     public static var flag:boolean=false;
     public static var flipedCard:boolean=false;


ScoreManager js code below, attached to my points GUIText object:

 public  var p:int;
 public  var p1:int;
 public var pointsClip: AudioClip;
 
 public var flag_:boolean;
 
 function Start(){
  p = 0;
  p1=0;
  guiText.text = "0";
  flag_ = false;
 }
 
 function Update () {
 
     if(Flip_card_1.flag == true || Flip_card_2.flag==true){
         p1 = 10;
         if(flag_ == false){
             audio.PlayOneShot(pointsClip);
             flag_ = true;
         }
     }
     p = p1;
     guiText.text = ""+ p;
 
 }

The problem is after the game over GUITexture (it brings me back to main menu and) reloads my scene again and the points GUIText shows the last points i got from my last score (ex. points=10) instead of showing my points back to 0 (as i set on Start() ). Why is that happening?

Comment
Add comment · Show 6
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 MakeITeasy · Dec 12, 2013 at 06:51 PM 0
Share

I set my variables p and p1 to static another way to say that when my scene starts again initialize both variables to 0.

 public  static var p:int=0;
 public  static var p1:int=0;

plus i put on comments function Start() But still it doesn't work though :(( Anything yet?

avatar image Sundar · Dec 12, 2013 at 07:18 PM 0
Share

Do the same in your card script

 function Start()
 {
      flag = false;
      flipedCard = false;
 
 }
avatar image fafase · Dec 12, 2013 at 07:20 PM 0
Share

He actually resets it all in the Start. Well, I would use normal variables during the scene and only if you win it then pass the value to a static variable.

avatar image Sundar · Dec 12, 2013 at 07:29 PM 0
Share

Fafase, I believe his Update() function is resetting that value. for some reason "if(Flip_card_1.flag == true || Flip_card_2.flag==true)" either one of this flag remains true. Hence he is getting p as 10 when loaded

avatar image MakeITeasy · Dec 12, 2013 at 07:45 PM 0
Share

Well, the answer was a combination of your answers (@OP_toss, @Sundar, @fafase) +1 for all of you.

Thanks a lot, all of you!

PS: @OP_toss thanx for ur advice i'll keep it in $$anonymous$$d for the future. Thank you again.

Show more comments

2 Replies

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

Answer by OP_toss · Dec 12, 2013 at 07:16 PM

you don't want static. Static variables don't get reset upon loading scenes and don't get cleaned up.

Are you loading your scene additively? Does OnDestroy ever get called on the score script? It looks like the only way this is possible is if you're not actually destroying and loading the scene again.

Personally, I prefer to manually reset the game, rather than depend on Unity's level loading to fix everything. It also becomes necessary if you have complex and load-heavy scenes. You don't want to make them wait 20 seconds every time they want to restart.

Just make a reset game routine/function that manually sets everything back where it should be, including the score. I like to have a Reset function in all my manager scripts. Then I have my GameManager reset all other managers in one go.

Hope this helps!

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
avatar image
0

Answer by guitarxe · Dec 12, 2013 at 07:56 PM

How are you reloading your scene? Remember that Start() is called only once in the lifetime of the script, when it is first enabled. If you don't ever disable and re-enable the script, then Start() will never be called again.

You could instead try putting your variable initialization into Awake(), which does get called whenever a script is re-initialized.

Furthermore, static variables are not destroyed when you reload a scene, since of course these belong to the Class and not an instance of the class.

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.Start.html http://answers.unity3d.com/questions/63241/reload-a-scene-and-resetting-all-variables.html

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 OP_toss · Dec 12, 2013 at 08:31 PM 0
Share

Yep, said all that above, and agreed. Start will only run once, and before any Update is called, so Update shouldn't be the problem.

But you're wrong about Awake. It is also only called once during the lifetime of a script, before Start. As per the docs:

Awake is called only once during the lifetime of the script instance.

OnEnable/OnDisable is called whenever a script is enabled/initialized or disabled, if that's what you're looking for.

I still think the right answer here is to reset the scene manually. It can't be more then setting a few variables. And it will happen much faster than it takes to reload the scene. Converting my comment above to an answer.

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

19 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how do i attach GUITEXT to a prefab 1 Answer

Score & High Score logic doesn't work 2 Answers

Display variables in GUIText? 1 Answer

Show score when enemy is destroyed C# 1 Answer

Why cant I subtract point from my score? 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