- Home /
It does not add money to player when it should ?
here is the code
bool intrigger; private void OnTriggerEnter(Collider other) { intrigger = true; } private void OnTriggerExit(Collider other) { intrigger = false; } private void Update() { if(intrigger) { if (Input.GetKeyDown(KeyCode.G)) { money = money + moneyonground; Debug.Log(money); gameObject.SetActive(false); } } } }
when i am in trigger of money (on ground) when i press g it says 5 there is no problem until here: when i am entering other money's trigger it still says 5 ? but is should say 10 ? can you help me
Answer by eskivor · Aug 13, 2017 at 10:35 AM
are you sure the moneyonground
variable inside your update gets the correct value ? check it with a Debug.Log("moneyonground " + moneyonground);
Answer by doleary-3800 · Aug 13, 2017 at 12:14 PM
Where is the variable money stored? if its just on the trigger item then you are just updating the amount on the item and each item would have their own amount. make sure the money variable is not stored on the trigger item and is on the script of the object causing the trigger.
example.. sorry but is in JS. //////coin script var playerObject : gameObject; var moneyonground : int = 5; var intrigger : boolean = false;
private void OnTriggerEnter(Collider other) { intrigger = true; playerObject = other.gameobject;}
private void OnTriggerExit(Collider other) { intrigger = false; }
function Update() { if(intrigger) { if (Input.GetKeyDown(KeyCode.G)) { playerObject.SendMessage("AddMoney",moneyonground); gameObject.SetActive(false); } } }
//// Add to player script var money : int = 0;
function AddMoney(amount : int){ money = money + amount; Debug.Log(money); }
Sorry i didn't change your ontrigger functions these should be
function OnTriggerEnter (other : Collider){ intrigger = true; playerObject = other.gameobject;}
function OnTriggerExit (other : Collider){ intrigger = false; }
Your answer
Follow this Question
Related Questions
Guides or Tutorials for a Very Basic Enemy AI? [Unity 5] 1 Answer
Make trigger be activated only by the player 2 Answers
Check if transform is in trigger 1 Answer
How to handle multiple colliders for one enemy and bullet. 3 Answers
How to start an animation of the object itself by trigger enter in #C? 1 Answer