Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

accessing bool from another script not working

Discussion in 'Scripting' started by Jeepster, Aug 17, 2019.

  1. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    401
    Hi,

    In my health script I set a bool to be true if the players health is zero. I do this to then access that bools state from the animation script and activate the death animation. I've written it correctly I think, and the bool does become true in the inspector when the player dies, but there's nothing happening from the animation scripts side. The debug message never shows. Here's my code:
    Health script essential code:
    Code (CSharp):
    1.     public bool dead1True;
    2.  
    3. ...
    4.     public void TakeDamage(float amt)
    5.     {
    6.        
    7.         currentHitPoints -= amt;
    8.  
    9.  
    10.         if (currentHitPoints <= 0)
    11.         {
    12.        
    13.             dead1True = true;
    14.         }
    15.    }
    animation script essential code:
    Code (CSharp):
    1.   public Health h;
    2.  
    3. ....
    4.  
    5.    void Update()
    6.     {
    7.  
    8.         if (h.dead1True == true)
    9.         {
    10.             _animator.SetBool("dead", true);
    11.             Debug.Log("DEADTRUE");
    12.         }
    13.     }
    Why isn't the bool "dead1True" accessed from my animator script? I'm referencing the Health script and I get 0 errors or hints as to why it doesn't work. Any help would be greatly appreciated, I really don't know what I'm missing here. Thank you
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,606
    A couple minor things to note before we get to your question:
    You dont need to compare booleans to true or false, simply use the bool itself:
    Code (CSharp):
    1. bool dead;
    2.  
    3. // Bad practice:
    4. if(dead == true) {...}
    5.  
    6. // Instead simply the bool value of dead, or !dead for the opposite:
    7. if(dead) {...}
    This is subjective, but you could also use a method to check whether the character is still alive, instead of constantly having to keep the dead bool updated:
    Code (CSharp):
    1. public bool IsDead(){
    2.     return currentHitPoits <= 0; // returns the bool value of this
    3. }
    You can still use the result of the method the same way you'd use the bool, for example "if(h.IsDead()){...}".

    Other than that your code looks fine, including the animator call. If your "DEADTRUE" Debug.Log gets printed but the bool in the animator does not get changed, then there is probably a problem with the animator that is not visible from the code shown.
    Like, for example, are you sure you are using the right animator component (maybe you are using the wrong animator)?
    Is the boolean you want to set actually named "dead" (maybe you used capital letters or underscores)?
    Did you double-check if the bool in the animator is really not changing? If it is you may have made a mistake inside the animator, that causes the animation to not get enabled based on the bools' state.

    Edit: I just re-read your post and saw that your debug message does not show up. If that's the case, but you see the bool of your character changing in the inspector, that either of these must be true:
    • Your animator script is not getting called. Is it actually attached to something? Test, for example, by printing a string in Start() or Update() outside any if-statements.
    • Or the bool referenced in the animator script does not contain the same value as the one you see changing in the inspector. This implies that the instance h is not actually the same you are viewing in the inspector. Make sure you set it up as you think you did.
     
    Last edited: Aug 17, 2019
    Jeepster likes this.
  3. LightningGravity

    LightningGravity

    Joined:
    May 29, 2019
    Posts:
    26
    You will just need to follow my old video to pass a variable into another script object.
     
    Jeepster likes this.
  4. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    401
    Thanks a lot for your help. There's been some strange development in the issue :D I changed the bool check to
    Code (CSharp):
    1. if (h.dead1True)
    instead of
    Code (CSharp):
    1. if (h.dead1True == true)
    to clean up my code, but apart from that I didn't change anything and now, when me player dies, the bool becomes true and the animation doesn't play. BUT if I set the dead1True boolean to true manually during play, it correctly activates the "dead" animation...

    Do you think this is a bug of some kind? because it proves that it works and there is a connection fra the health script bool check to the animationscript and the animator itself.
     
    LightningGravity likes this.
  5. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    401
    I figured out the problem: I had contradicting animation orders in my animation script that created a conflict. Thank you both for your help ;-)
     
    Yoreki likes this.