0
Q:

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;
    }
}
9
public class Script1 : MonoBehavior
{
public static int Script1Int;
}
//Another script:

public class Script2 : MonoBehavior
{
public static int Script2Int;

	void Start()
	{
    	Script2Int = Script1.Script1Int;
    }
}
3
 public class PointScript : MonoBehaviour {      public static int playerScore;  //  Static keyword makes this variable a Member of the class, not of any particular instance.      void OnTriggerEnter2D(Collider2D other)     {         if (other.tag == "Player")             playerScore++;  //  The code that any instance can use to cause the score to be incremented, since the playerScore variable is a Static member, all instances of this class will have access to its value regardless of what instance next updates it.     } }
0

New to Communities?

Join the community