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 access variables from different scripts?

Discussion in 'Scripting' started by AmitRon, Jun 15, 2014.

  1. AmitRon

    AmitRon

    Joined:
    Jun 15, 2014
    Posts:
    3
    Hi!

    I have a script named "Damage Controller".

    Inside of the script I have a variable called "hitPoints" which indicates the amound of health the object has.

    I want to show as a text above the object it's health/hitPoints.

    To do so, I need to make a custom script for the text (correct me if I am wrong).

    How can I access the "hitPoints" variable from the "Damage Controller" script to the text's script?
     
  2. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    There are a few solutions.
    1. Have Damage Controller use an interface, and once you do so, you can have a variable that is the damage controller.

    Code (CSharp):
    1. DamageController implements iControlDamage
    2.  
    3. iControlDamage damageController = object.GetComponent(iControlDamage);
    4.  
    5. damageController.getHP();
    2. Another way that I believe is not really liked, is "Send Message", you can use it like so;
    object.SendMessage("getHP");

    I don't remember if it returns a value, but it might...

    I write these off the top of my head, so they are not accurate, but they should give you an idea.
     
  3. AmitRon

    AmitRon

    Joined:
    Jun 15, 2014
    Posts:
    3
    Thanks for the reply :)

    What do you mean when you say "Have Damage Controller use an interface"?
     
  4. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    An interface is a piece of code that only contains methods, but not their code.
    Once a class implements an interface, it must also implement a code for each of those methods.

    So if you have a few different damageable objects, such as a tank, and a human, you have them implement "iDamageable" which which will have a method named "public void ReceiveDamage(float dmg)"

    Now when you want to attack the vehicle, no matter which vehicle it is, you just use "iDamageable target = object.GetComponent<iDamageable>();" and then, "target.ReceiveDamage(100f);"

    What this means, is that you do not have to have a something like;

    "If target == human, use method hurttarget, else if target == tank, use method calculate armor penentration, then hurttarget"

    But take care of all of these things under the same class

    Tank's ReceiveDamage will contain code to calculate armor pen, damage, etc, while human will contain code to calculate wounds and damage.

    Which means that the code for the projectile is very simple; target.ReceiveDamage.

    I'm not good at explaining myself, so sorry for that, but basically, it allows you to use one method for all sorts of objects, and since they inherit an interface, you can also be sure they indeed have that method.

    I suggest you take a look at it.
     
  5. AmitRon

    AmitRon

    Joined:
    Jun 15, 2014
    Posts:
    3
    THanks :D