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. Dismiss Notice

boolean problem

Discussion in 'Scripting' started by VAN-D00M, Oct 1, 2014.

  1. VAN-D00M

    VAN-D00M

    Joined:
    Dec 24, 2013
    Posts:
    44
    I'm trying to create an invincibility power up. I have a DestroyByContact script on each of my enemies, the invincibility power up calls a function in the DestroyByContact script which turns the invincible bool to true. Then self explanatory in the OnTriggerEnter part when collider.other tag == "Player". if statement checks if bool is false, if it is proceed as usual, if true then not. Sounds simple enough.

    invincible turns true, no problem. but it only stays true for 1 collision then it turns itself back to false. This confuses me as there is no other conflicting code otherwise. I don't know if something like this is supposed to work. When I planned it out, this seemed the best and only way to achieve invincibility. I really didn't want to bother anyone with this, I found out what is going wrong but don't know how it is. I would appreciate any help offered. Cheers


    DestroyByContact

    Code (CSharp):
    1. void OnTriggerEnter (Collider other)
    2.     {
    3.  
    4.         if (other.tag == "Boundary" || other.tag == "Enemy" || other.tag == "Hazard")
    5.         {
    6.             return;
    7.         }
    8.            
    9.         if (explosion != null)
    10.         {
    11.             Instantiate(explosion, transform.position, transform.rotation);
    12.             //Destroy (other.gameObject);
    13.         }
    14.  
    15.         if (other.tag == "Player")
    16.         {
    17.             Debug.Log(invincible);
    18.  
    19.             if(invincible == false)
    20.             {
    21.                 Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
    22.  
    23.                 Destroy (other.gameObject);
    24.                 Debug.Log(invincible);
    25.                 gameController.GameOver();
    26.                    
    27.             }
    28.  
    29.         }
    30.  
    31.  
    32.         gameController.AddScore(scoreValue); //calls public AddScore function
    33.  
    34.         Destroy (gameObject);
    35.      }
    36.  
    37.  
    38.  
    39. public void Invincible(float countdown)
    40.     {
    41.         invincible = true;
    42.         Debug.Log(invincible);
    43.     }
    44.         }

    This is how DestroyByContact worked before I messed with it incase that doesn't make sense.

    Code (CSharp):
    1. void OnTriggerEnter (Collider other)
    2.     {
    3.  
    4.         if (other.tag == "Boundary" || other.tag == "Enemy" || other.tag == "Hazard")
    5.         {
    6.             return;
    7.         }
    8.            
    9.         if (explosion != null)
    10.         {
    11.             Instantiate(explosion, transform.position, transform.rotation);
    12.  
    13.         }
    14.  
    15.         if (other.tag == "Player")
    16.         {
    17.            
    18.             Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
    19.  
    20.             gameController.GameOver();
    21.                    
    22.         }
    23.  
    24.  
    25.             Destroy (other.gameObject);
    26.             Destroy (gameObject);
    27.  
    28.  
    29.  
    30.     }
     
  2. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    You would be better off if you handled all things invincibility within your player's class. I imagine something like this:

    Code (CSharp):
    1. class Player {
    2.     ...
    3.     private bool _invincible;
    4.  
    5.     public void setInvincible(bool invincible) {
    6.         this._invincible = invincible;
    7.     }
    8.  
    9.     public bool getInvincible() {
    10.         return this._invincible;
    11.     }
    12.     ...
    13. }
    Then, in the OnTriggerEnter for your powerup, you would do something like this:

    Code (CSharp):
    1. void OnTriggerEnter(Collider other) {
    2.     if (other.GetComponent<Player>() == null) {
    3.         return;
    4.     }
    5.     var player = other.GetComponent<Player>();
    6.     player.setInvincible(true);
    7. }
    Finally, the last thing you'd need to do is in your DestroyByContact script you posted above.

    Code (CSharp):
    1. void OnTriggerEnter(Collider other) {
    2.     if (other.GetComponent<Player>() != null) {
    3.         var player = other.GetComponent<Player>();
    4.         if (player.getInvincible() == false) {
    5.             //damage / destroy the player
    6.         }
    7.     }
    8. }
     
    VAN-D00M and SniperEvan like this.
  3. VAN-D00M

    VAN-D00M

    Joined:
    Dec 24, 2013
    Posts:
    44
    wow, what a great way of doing it. Never considered doing it like that. Thanks a lot for that! :D
     
  4. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    No problem! Glad I could help. Let me know if you have any questions regarding this design.