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 Saskiakr · Feb 23, 2014 at 09:38 PM · load scenerestartrestart gamestatic variable

scene won't reset after restart

Hi,

I'm new to Unity and have a problem with the game I'm trying to make. I have 4 scenes. "Menu", "Game", "You win" and "Game Over". Basically a car is driving around and when 10 coins get collected the "You Win" screne appears. There I added a button which lets you restart the game.

The problem I'm having is that whenver I press the restart button the "Game" scene won't go back to its original state, which means that the 10 coins are still colelcted and therefore the "You Win" screen appears immediately after I press the restart button.

This is the code:

ragma strict

static var coinCounter = 0;

function Start () {

}

function Update () { if(coinCounter == 10){ Application.LoadLevel("You Won"); }

}

function OnCollisionEnter (collision : Collision) { if(collision.transform.name == ("Coin")) { coinCounter += 1; } }

I added all scenes to the build settings so that's not the problem. Does this maybe have anything to do with the static variables?

Thanks A LOT for any help. :)

Saskia

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

Answer by YoungDeveloper · Feb 23, 2014 at 09:43 PM

You shouldn't really use static for such simple things. And to me it looks like you can only collect 1 coin before level you won is loaded, because of:

  if(coinCounter == 1){ Application.LoadLevel("You Won");

And yes, static variable is mostly the cause, either set it back on awake, or even better, dont use static.

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 Saskiakr · Feb 23, 2014 at 09:45 PM 0
Share

Yeah sorry I changed it to 1 for testing reasons. Can you show me how to code this without static?

avatar image YoungDeveloper · Feb 23, 2014 at 09:54 PM 0
Share

This should be fine. Couple notes though, i suggest using tags, ins$$anonymous$$d of comparing gameobject names, because by that you can have multiple items under one tag "pickable", or other. There is no need to have empty start or update methods if you dont use them. Also, i think you should destroy the coin after you have counted it in.

 var amount: int = 0;
 
 function Update(){ 
     if(coinCounter == 10){ 
         Application.LoadLevel("You Won"); 
     }
 }
 
 function OnCollisionEnter(collision : Collision){
     if(collision.transform.name == "Coin"){ 
         coinCounter++; 
         Destroy(col.gameObject); //or just col, because it return gameobject
     }
 }

And format your code next time please, there is a button "1010" )

avatar image Saskiakr · Feb 25, 2014 at 12:48 AM 0
Share

Thanks very much!!! Yeah I am actually destroying the coins, just didn't copy that part of the code and I'll make sure to format next time. :) Everything works now! :)

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

,Restart Scene after level Completion 0 Answers

i have a button to restart the same scene but it dont works (with images) 7 Answers

Restart Game from Button, not SceneMangement 1 Answer

Current Level Load Again After Level Completion 2 Answers

how to stop the whole game but not the death animation when player dies 3 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