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 LasseBmann · Oct 10, 2011 at 07:30 PM · inputtriggercoroutinevaluevalues

Scripting problem, with values and input.getbutton

Hey all, I've been tinkering around a tiny scavenging system for a game i'm working on - at this moment. All it i supposed to do is add a certain amount of food/scrap to a value in my gameplayglobals script if i press the scavenge button while inside the triggerzone attached to a crate - and then destroy the crate. It works, i get (in this case food) added to my globals - BUT i get it added about 22 times, instead of just adding one "portion" of 1-4 food.

 var IsFoodContainer : boolean = false;
 var IsScrapContainer : boolean = false;
 static var FoodAmount = Random.Range(1,4);
 static var ScrapAmount = Random.Range(1,5);
 
 function OnTriggerStay (other : Collider) {
 // IF player scavenges while in zone
 if (Input.GetButtonDown ("Scavenge")&& IsFoodContainer == true){
 AddFood();
 }
 
 // If player rolls 2, roll amount, and goto addscrap,
 if (Input.GetButtonDown ("Scavenge") && IsScrapContainer == true){
 AddScrap();
 }
 
 }
 
 function AddFood(){
 GameplayGlobals.foodAmount += FoodAmount;
 Debug.Log("ADDED FOOD"+FoodAmount);
 Destroy(gameObject);
 }
 //////////////////////////////////////////////////////////////////////////////////////
 function AddScrap(){
 GameplayGlobals.scrapAmount += ScrapAmount;
 Debug.Log("Added SCRAP"+ScrapAmount);
 
 Destroy(gameObject);
 }



I really hope someone can help me here - i've tried all the different "Input.GetButton" variations (up/down) - tried rearranging the code in different matter. I've been staring blindly at the code for a while now. So I hope someone knows what to do:)

Thanks in advance. Lasse

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 aldonaletto · Oct 10, 2011 at 08:55 PM

You should read Input.GetButtonDown only inside Update: it will return true during all the update cycle. If you read in FixedUpdate, OnTriggerStay, OnGUI - routines that are called at different paces - you may have this bad effect. You must change your logic a little: set a boolean variable at OnTriggerEnter and reset it at OnTriggerExit, and check the variable inside Update:

var inTrigger = false;

function OnTriggerEnter(other: Collider){ if (other.tag == "Player) inTrigger = true; }

function OnTriggerExit(other: Collider){ if (other.tag == "Player) inTrigger = false; }

function Update(){ if (inTrigger && Input.GetButtonDown("Scavenge"){ if (IsFoodContainer){ AddFood(); } if (IsScrapContainer){ AddScrap(); } } } NOTE: If IsFoodContainer and IsScrapContainer are both true, the player will do both things, because Destroy will take effect only after the current Update cycle.

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 LasseBmann · Oct 10, 2011 at 10:11 PM 0
Share

Thanks man, $$anonymous$$uch appreciated:D I guess you learn something new every day^^

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Great unity X Y value (Space game) 0 Answers

Updating a Value in OnTriggerEnter and Returning That Value Immediately... 0 Answers

input button while in a trigger? 2 Answers

Input.getKeyDown alternative for XR Devices 1 Answer

Lightswitch help? 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