Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 EvanCheddar · Feb 01, 2018 at 07:04 PM · instantiatevariablevariablesintreset

How to reset a variable?

Hello all, I want to reset my variable int back to "8" after my SphereKILL is instantiated but I cant figure out how. Here's my script:

 public GameObject SphereKILL;
     public int Counter = 8;
     public int FullCounter;
     public float timer = 30f;
 
     void Update ()
     {
         timer -= Time.deltaTime;
 
         {
 
             if (timer <= 0f)
             if (Counter >= 0)
     Instantiate (SphereKILL, transform.position, transform.rotation);
             
             
                 if (timer <= -1f)
                     timer = 30f;
             
                 if (Counter <= -1)
                     Time.timeScale = 0;
             }
         }
 
     public void CountDoody(int howmany)
     {
         Counter -= howmany;
     }
 
     }

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 Glurth · Feb 01, 2018 at 07:21 PM

You'll need a value to compare against. You could create another, private reference to the SphereKill object.

 private GameObject lastSphereKill;

Inside update, compare your current SphereKill reference with this private one.

 if(SphereKill !=lastSphereKill)
 {
    Counter = 8;
    lastSphereKill=SphereKill;
 }

Note, this will reset Counter to 8 even if you just delete the reference to SphereKill, setting it to null. Some additional If's could guard against that, if necessary. Edit: Also note, you could do the same thing with an "accessor" to the SphereKill, eliminating the need to store another reference. Alas, accessor's don't play well with Unity's inspector UI and serialization.

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 EvanCheddar · Feb 01, 2018 at 08:03 PM 0
Share

Thank you for taking the time to respond. I just tried to incorporate your code inside Update but it didn't work. Let me explain further. I want to Instantiate the Sphere$$anonymous$$ILL every 30 seconds and if there are >= 0 objects inside the Sphere$$anonymous$$ILL then destroy the game objects but if there are <-1 then it's game over. The Sphere$$anonymous$$ILL has a script on it to destroy itself after 2 seconds. The only problem is, the Counter needs to go back to 8 in order for the game to make sense. The counter isn't a life bar, it's only suppose to represent the amount of objects in the Sphere$$anonymous$$ILL. Does that make sense?

avatar image Glurth EvanCheddar · Feb 01, 2018 at 08:57 PM 0
Share

I think you may have some other issues with your Update function. for example: when is CountDoody called? I suggest heavy use of Debug.Log()'s. Always useful to see what you are TELLING it to do, vs what you WANT it to to.

avatar image EvanCheddar Glurth · Feb 01, 2018 at 09:17 PM 0
Share

So doodycount is called when a certain gameobject collides with the gameobject that has the script I posted on it. I should use Debug.Log() more, yes. Any other suggestions on how to reset that variable?

Show more comments

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

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

Related Questions

Variable not being saved? 0 Answers

Problem naming instances with incrementally numbered counter 1 Answer

int variable loses its value 0 Answers

Script is setting variables but they're being reset 1 Answer

Problem with variables,Problem with identifying a variable 2 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