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 · Mar 26, 2018 at 04:40 PM · variablesupdate functionaddbasic programmingprojectiles

How can I add to an existing variable only once?

Hello all. I have an inventory of darts in my game that starts out as 50. When "round 2" begins, I want to add 50 more darts to whatever total is leftover from "round 1" for example: if I have 10 leftover from round 1, I want to have 60 to start round 2. My code now reads: if round == 2, darts.dartvalue +=50. the problem is that it updates my inventory for like 4 seconds while "round 2" is being displayed and I end up with 5000 darts. How can I just add 50 once to my overall total? Thank you for helping!

Comment
Add comment · Show 2
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 RobAnthem · Mar 26, 2018 at 04:50 PM 0
Share

Well it's obvious that you put your code in the update loop, this is your problem. $$anonymous$$ove it to a function and call it once, or if round 2 is in a new scene, move it to OnEnable or Start().

Otherwise you can make some janky system like this....

 public int DartCount
 {
     get
     {
         return _dartCount;
     }
     set
     {
         if (round > lastRound)
        {
            round = lastRound;
            _dartCount = value;
        }
     }
 }
 private int _dartCount;
 private int lastRound = 1;
avatar image Martin_Gonzalez · Mar 26, 2018 at 07:55 PM 0
Share

Why not do a method called ResetRound() that will add the darts to players?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by NoMoneys · Mar 26, 2018 at 04:56 PM

You could check if the 50 points have been added with a boolean variable that turns to false when the 50 darts have been added. Like this:

 bool readyToAddPoints = true;
 
 if(readyToAddPoints)
 {
 darts.dartsvalue += 50;
 readyToAddPoints = false;
 
 }

Then whenever you have to update the darts value again, just set the bool to true.

Comment
Add comment · Show 6 · 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 · Mar 26, 2018 at 05:33 PM 0
Share

Hey thank you, that worked. I just had to put it inside a function then call that function and it works perfectly.

avatar image RobAnthem EvanCheddar · Mar 26, 2018 at 06:02 PM 0
Share

This solution only works if you only have 2 rounds or aren't resetting your readyToAddPoints bool.

avatar image EvanCheddar RobAnthem · Mar 26, 2018 at 06:28 PM 0
Share

oh shoot, you're right! What should I do?

Show more comments
avatar image EvanCheddar · Mar 26, 2018 at 07:54 PM 0
Share

having a hard time resetting it b/c the rounds are all in the same scene

avatar image RobAnthem EvanCheddar · Mar 26, 2018 at 07:57 PM 0
Share

Don't forget about my getter/setter solution, that accommodates exactly your situation.

avatar image
0

Answer by EvanCheddar · Mar 26, 2018 at 08:49 PM

The cleanest/easiest solution I came up with was to call the static variable via an animation event that happens only if you make it to the next round therefore calling a function that adds more darts to the player inventory. Thank you all for your help, to anyone looking at this in the future, there are some real solutions here. All the best, take care!

Comment
Add comment · Show 8 · 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 Martin_Gonzalez · Mar 26, 2018 at 08:51 PM 2
Share

Sorry to tell you but that is not a clean way to do it. But if it works for you... :|

avatar image EvanCheddar Martin_Gonzalez · Mar 26, 2018 at 08:55 PM 0
Share

seems pretty easy/clean, what would be considered a cleaner solution?

avatar image fafase EvanCheddar · Mar 27, 2018 at 07:39 AM 1
Share

There is nothing clean in using static to fix a problem that should be fixed in a different way.

avatar image Martin_Gonzalez · Mar 26, 2018 at 08:58 PM 0
Share

I would recommend to set states to your Game. You are talking about rounds, so you can easily create a Game class with the different states of it Prepare, Reset, Start, Pause, Resume, Over so you can manipulate data from the class and not from an animation. You will be working on this and in some weeks you are going to be screa$$anonymous$$g who the hell is giving darts to the players :P

avatar image EvanCheddar Martin_Gonzalez · Mar 26, 2018 at 09:07 PM 0
Share

Yeah you might be right, the game is fairly simple so hopefully I can get by this way. It's my first game so I'm learning a lot. Im already towards the end of development so I just have to make do with the some what sloppy code right now. For my next game, I'll be much neater and organized that's for sure.

avatar image Martin_Gonzalez EvanCheddar · Mar 26, 2018 at 09:13 PM 0
Share

Then you go! Good luck with your development! :D

Show more comments
avatar image EvanCheddar · Mar 27, 2018 at 04:36 PM 0
Share

Hey guys come on, it's just a unity animation event on some basic game, who cares?

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

80 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

Related Questions

Function is not updating the variable 1 Answer

How to get in sync two scripts if the Update () calls are random? 2 Answers

Variable goes very high in update method 1 Answer

Accessing variables from GameObjects in an array 3 Answers

Calling a mesh instance 'mesh' or 'msh' (to avoid the keyword) 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