get int from another script unity code example
Example 1: unity variable from another script
public class PlayerScript: MonoBehaviour {
public float Health = 100.0f;
}
public class Accessor : MonoBehaviour {
void Start()
{
GameObject thePlayer = GameObject.Find("ThePlayer");
PlayerScript playerScript = thePlayer.GetComponent<PlayerScript>();
playerScript.Health -= 10.0f;
}
}
Example 2: how to reference a variable from another script in unity
public class PointScript : MonoBehaviour { public static int playerScore;
Example 3: get int from another script unity
public SriptName MyScript; void Start() { MyScript = GameObject.Find(“NameOfObjectWithScript”).GetComponent<ScriptName>(); } void Update() { Debug.Log(MyScript.MyInt); }