6

For example I have a variable (public static float currentLife) in script "HealthBarGUI1" and I want to use this variable in another script. How do I pass variable from one script to another C# Unity 2D?

MickyD47
  • 61
  • 1
  • 1
  • 3

4 Answers4

9

You could do something like this, because currentLife is more related to the player than to the gui:

class Player {

  private int currentLife = 100;

  public int CurrentLife {
    get { return currentLife; }
    set { currentLife = value; }
  }

}

And your HealthBar could access the currentLife in two ways.

1) Use a public variable from type GameObject where you just drag'n'drop your Player from the Hierarchy into the new field on your script component in the inspector.

class HealthBarGUI1 {

  public GameObject player;
  private Player playerScript;

  void Start() {
    playerScript = (Player)player.GetComponent(typeof(Player)); 
    Debug.Log(playerscript.CurrentLife);
  }

}

2) The automatic way is achieved through the use of find. It's a little slower but if not used too often, it's okay.

class HealthBarGUI1 {

  private Player player;

  void Start() {
    player = (Player)GameObject.Find("NameOfYourPlayerObject").GetComponent(typeof(Player));
    Debug.Log(player.CurrentLife);
  }

}

I wouldn't make the currentLife variable of your player or any other creature static. That would mean, that all instances of such an object share the same currentLife. But I guess they all have their own life value, right?

In object orientation most variables should be private, for security and simplicity reasons. They can then be made accessible trough the use of getter and setter methods.

What I meant by the top sentence, is that you also would like to group things in oop in a very natural way. The player has a life value? Write into the player class! Afterwards you can make the value accessible for other objects.

Sources:

https://www.youtube.com/watch?v=46ZjAwBF2T8 http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Find.html http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html

Wipster
  • 1,510
  • 1
  • 15
  • 32
  • It's also worth considering using an Entity-Component-System pattern. With this you wouldn't have a Player class at all, but rather properties (or components in Unity3D terms) of which the Player consists. For example a Health Component. This could then also be reused for Enemies and destroyable objects. https://en.wikipedia.org/wiki/Entity%E2%80%93component%E2%80%93system – Wipster May 09 '17 at 08:07
0

You can just access it with:

HealthBarGUI1.currentLife

I'm assuming that HealthBarGUI1 is the name of your MonoBehaviour script.

If your variable is not static and the 2 scripts are located on the same GameObject you can do something like this:

gameObject.GetComponent<HealthBarGUI1>().varname;
Radu Diță
  • 13,476
  • 2
  • 30
  • 34
  • What if they are are on two separate objects? is it going to use: gameObject.GetComponent.varname; – MickyD47 Apr 08 '14 at 05:58
  • if the var is static you can just use the first variant, it the var is not static then you need to add a reference to the first game object in the second and then you can access it like this firstGameObject.GetComponent().varname – Radu Diță Apr 08 '14 at 06:16
0
//Drag your object that includes the HealthBarGUI1 script to yourSceondObject place in the inspector.

public GameObject yourSecondObject;

class yourScript{

    //Declare your var and set it to your var from the second script.
    float Name = yourSecondObject.GetComponent<HealthBarGUI1>().currentLife;

}
Shak1234
  • 54
  • 1
  • 7
0

TRY THIS!

//Drag your object that includes the HealthBarGUI1 script to yourSceondObject place in the inspector.

public GameObject yourSecondObject;

class yourScript{

    //Declare your var and set it to your var from the second script.
    float Name = yourSecondObject.GetComponent<HealthBarGUI1>().currentLife;
Flair
  • 2,609
  • 1
  • 29
  • 41