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 greatwhiteshark17283 · Jun 06, 2013 at 08:07 PM · variablevaluerepeat

How to make a variable it's original value?

I made a script that counts the number of times and object collides with it and when it reaches 0, the object is destroyed and later teleported to another location. Here is the script: var lives = 5; var spawnPoint : Transform; var seconds = 2.0;

 function OnTriggerEnter(){
     (lives) = (lives) - 1;
     if((lives) == 0){
        Invoke("LiveAgain", (seconds));
        gameObject.SetActive(false);
     }
 }
  
 function LiveAgain() {
     gameObject.transform.position = spawnPoint.position;
     gameObject.SetActive(true);
 }

How could I make it so then the variable "lives" is changed back to it's original value after spawning again?(For example, if lives was originally ten, I would want it to be 10 after it spawns again at "spawnPoint")

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 Dave-Carlile · Jun 06, 2013 at 08:08 PM

Just set the variable to the value you want in your LiveAgain function.

 function LiveAgain()
 {
   lives = 10;    // <-- just set the variable back to the value you want
   gameObject.transform.position = spawnPoint.position;
   gameObject.SetActive(true);
 }
Comment
Add comment · Show 4 · 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 greatwhiteshark17283 · Jun 06, 2013 at 08:24 PM 0
Share

Thanks, but you didn't get what I meant. You did give me an idea, though. I edited the script and if you want to know what I was trying to do, here is the script:

 var lives = 5;
 var spawnPoint : Transform;
 var seconds = 2.0;
 private var hearts = 0;
 
 function OnTriggerEnter(){
     (lives) = (lives) - 1;
     (hearts) = (hearts) + 1;
     if((lives) == 0){
        Invoke("LiveAgain", (seconds));
        gameObject.SetActive(false);
     }
 }
  
 function LiveAgain() {
     (lives) = (hearts);
     (hearts) = 0;
     gameObject.transform.position = spawnPoint.position;
     gameObject.SetActive(true);
 }
avatar image Dave-Carlile · Jun 06, 2013 at 08:32 PM 0
Share

How is my answer different from what you asked for in your question?

avatar image greatwhiteshark17283 · Jun 06, 2013 at 11:56 PM 0
Share

Because if I did your script, I would need to change the number of lives every time I made a game object. This way, I made a private var so then the number of lives is always the same as it was in the beginning.

avatar image Dave-Carlile · Jun 07, 2013 at 12:16 AM 0
Share

That's fine, but that wasn't part of your original question, so I'm not sure why you said "I didn't get what you meant", because I answered the question you asked. In any case, it's irrelevant. Glad I was able to help :)

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

15 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

Related Questions

Can I show a numeric variable from the slider on the GUI? 1 Answer

cant assign variables value 2 Answers

Maintain variable values while changing levels 1 Answer

Assign a random value to var personality" upon asset creation (Java) 1 Answer

When passing a value it becomes null 0 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