Question by
dagim_tekle · Oct 16, 2016 at 02:05 PM ·
collisionint
How do you add values of int healthValue on collision with a gameObject Coin in a 3d world??
I was making a 3d game and I wanted to add the health value number when you collide with the coin.
Comment
Best Answer
Answer by Gothic_Anatomist · Oct 16, 2016 at 02:21 PM
You can add an increment to the health value within the OnTriggerEnter function. There's a great tutorial on the Unity site https://unity3d.com/learn/tutorials/projects/roll-ball-tutorial for collecting cubes. You could implement the increment in a number of ways:
**adapted from mentioned tutorial**
//declare health variable int
public int healthValue;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Pick Up"))
{
other.gameObject.SetActive (false);
// perform the increment +1
healthValue++;
}
}
or if you want to do it a set amount each time
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Pick Up"))
{
other.gameObject.SetActive (false);
// perform the increment +30
healthValue += 30;
}
}
I hope that helps :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612094735im_/https://answers.unity.com/themes/thub/images/avi.jpg)