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

how to refere scriptts

Discussion in 'Getting Started' started by hrohibil, May 28, 2021.

  1. hrohibil

    hrohibil

    Joined:
    Apr 17, 2021
    Posts:
    267
    I have a TakeDamage script and a Player script.

    In my TakeDamage script, I have function to test how to recive damage by pressing the down button and I alsp tried with an object. That worked fine.

    But in my player script I have all the animations setup.

    How can i refere to the currentHealth which is in my TakeDamage script, into my PLayer script?

    I want when currentHelath is zero then i play a dead animation..
     
  2. max3210

    max3210

    Joined:
    Apr 19, 2020
    Posts:
    17
    This isn't a great programming practice, but for the small and simple games I've been making I just use a global script with public variables for things like player health. These can be accessed from any other script.
     
    Last edited: May 29, 2021
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,842
    In many cases the best way to handle this is with Unity events. Your TakeDamage script should have some public event on it like "onDeath" which it calls when the health goes <= 0. And your Player script should have a public method on it like "Die". Then, in the inspector for your player game object, you can connect the onDeath event up to the Die method.

    This blog post/video on Unity Events may help make all that clear.
     
    max3210 likes this.