[SOLVED]Creating droppable objects

Hello guys,
I thought that I would develop some droppable objects in my game like hp shields and special attacks that you get if the enemy random drops them… I decided to start adding them one by one as to avoid balancing issues. However no matter how I write the code the moment I place the Hpdrop prefab within the game I become either immortal or I keep getting healed every frame doesnt matter if I’m actually colliding with the hpdrop prefab.
What I am saying in my code is basically :

void OnTriggerEnter2D ()
{
        if (GameObject.FindWithTag ("Enemy")) {
            CurrentHealth -= 1;
      
        }
        if (GameObject.FindWithTag ("HealthDrop")) {
            CurrentHealth += 1;
        }


        if (GameObject.FindWithTag ("Lightning")) {
            StartCoroutine (Timer());
        }

        if(invulnPeriod > 0) {
            invulnTimer = invulnPeriod;
            gameObject.layer = 10;
        }
}

Now the Lightning and the Enemy game objects work perfectly. If I get hit by lightning(this is only for the final lvl) I get destroyed after a 0.2 second CD (as my per my coroutine), the part also works perfectly if I collide with an enemy or an enemy attack, I lose 1 health. But the moment I add the droppablehp prefab everything goes on the fritz. Ive tried many ways to write the code such as using else statements and introducing a new coroutine to specifically handle the droppable health none of them worked properly like I said either I become immortal while the droppablehp object is in the game or I gain health every frame regardless of whether Im colliding with the object or not… Any help would be appreciated!

You are not checking if you collided with an object that has a specific tag. You are instead checking if any object in the scene has that tag.

1 Like

It worked perfectly thank you very much.
P.S. for any1 else that might run into a similar problem the correct code in my case was:

void OnTriggerEnter2D (Collision2D other)   //You can name the collision2D anything you want
{
if (other.gameObject.tag == "Enemy") {               // These are the names of my tags of course so youd have to put your
CurrentHealth -= 1;                                              // own in there same goes for CurrentHealth

}
if ( other.gameObject.tag == "Healthdrop") {
CurrentHealth += 1;
}


if (other.gameObject.tag == "Lightning") {
StartCoroutine (Timer());
}

if(invulnPeriod > 0) {
invulnTimer = invulnPeriod;
gameObject.layer = 10;
}
}
1 Like

please use [code ][/code ] tags when pasting code into the forums, there is a sticky on them at the top of the scripting forum.

fixed :slight_smile: