Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[SOLVED]Creating droppable objects

Discussion in 'Scripting' started by Bolchev, Aug 20, 2015.

  1. Bolchev

    Bolchev

    Joined:
    Aug 3, 2015
    Posts:
    17
    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 :
    Code (csharp):
    1.  
    2. void OnTriggerEnter2D ()
    3. {
    4.         if (GameObject.FindWithTag ("Enemy")) {
    5.             CurrentHealth -= 1;
    6.      
    7.         }
    8.         if (GameObject.FindWithTag ("HealthDrop")) {
    9.             CurrentHealth += 1;
    10.         }
    11.  
    12.  
    13.         if (GameObject.FindWithTag ("Lightning")) {
    14.             StartCoroutine (Timer());
    15.         }
    16.  
    17.         if(invulnPeriod > 0) {
    18.             invulnTimer = invulnPeriod;
    19.             gameObject.layer = 10;
    20.         }
    21. }
    22.  
    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!
     
    Last edited: Aug 20, 2015
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    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.
     
    Bolchev likes this.
  3. Bolchev

    Bolchev

    Joined:
    Aug 3, 2015
    Posts:
    17
    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:

    Code (csharp):
    1.  
    2. void OnTriggerEnter2D (Collision2D other)   //You can name the collision2D anything you want
    3. {
    4. if (other.gameObject.tag == "Enemy") {               // These are the names of my tags of course so youd have to put your
    5. CurrentHealth -= 1;                                              // own in there same goes for CurrentHealth
    6.  
    7. }
    8. if ( other.gameObject.tag == "Healthdrop") {
    9. CurrentHealth += 1;
    10. }
    11.  
    12.  
    13. if (other.gameObject.tag == "Lightning") {
    14. StartCoroutine (Timer());
    15. }
    16.  
    17. if(invulnPeriod > 0) {
    18. invulnTimer = invulnPeriod;
    19. gameObject.layer = 10;
    20. }
    21. }
    22.  
     
    Last edited: Aug 20, 2015
    ThermalFusion likes this.
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    please use [code ][/code ] tags when pasting code into the forums, there is a sticky on them at the top of the scripting forum.
     
  5. Bolchev

    Bolchev

    Joined:
    Aug 3, 2015
    Posts:
    17
    fixed :)