Answers for "unity access variable from another script c#"

C#
20

unity variable from another script

//Make Health public
public class PlayerScript: MonoBehaviour {
  public float Health = 100.0f;
}

//Access it.

public class Accessor : MonoBehaviour {
    void Start()
    {
        GameObject thePlayer = GameObject.Find("ThePlayer");
        PlayerScript playerScript = thePlayer.GetComponent<PlayerScript>();
        playerScript.Health -= 10.0f;
    }
}
Posted by: Guest on April-20-2020
3

unity call variable from another script

// Access non-static variables from other scripts
public class A : MonoBehaviour // Script where variable is located:
{
	public static float num = 0;
}
public class B : MonoBehaviour // Script where variable is accessed:
{
	public A scriptA;
	void Start()
    {
    	if (scriptA.num == 0)
        {
        	Debug.Log("num is 0");
        }
    }
}
Posted by: Guest on September-19-2021

Code answers related to "unity access variable from another script c#"

C# Answers by Framework

Browse Popular Code Answers by Language