Change a Int on collision or trigger

so im somewhat new to unity and i was wondering if i could do

OnCollisionEnter(Collision Col)
{
// this is the part i need help with
if(player.tag=Player)
{
Change int gunDamage +1
Destroy GameObject gunDamageUp
}

the name of the value i want to change is gunDamage

Hi @whycantipickaname ,

You can definitely do that. Your code will look like this:

void OnCollisionEnter(Collision collision)
{
    if (collision.gameObject.CompareTag("Player"))
    {
        gunDamage++;
        Destroy(gunDamageUp);
    }
}

You can find more information about CompareTag here: Unity - Scripting API: Component.CompareTag

Happy learning!

P.D. I have some recommendations on how you’re coding:

  • You should make sure that gunDamage and gunDamageUp variables are of type int and GameObject respectively and are declared on the same class in where the OnCollision method is being used.
  • Also, you need to make sure that the variable names that you’re using are descriptive enough of what you’re doing.
  • Finally, you can consider adding some comments to your code, because right now, it is impossible to know what you’re trying to accomplish inside of your if block.
1 Like

wow thats a lot of info thanks very much for the help

1 Like