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?

0

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

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. en.wikipedia.org/wiki/Entity%E2%80%93component%E2%80%93system
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;

2 Comments

What if they are are on two separate objects? is it going to use: gameObject.GetComponent<HealthBarGUI1>.varname;
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<HealthBarGUI1>().varname
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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.