change variable in 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: unity how to use variable from another script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Miner : MonoBehaviour
{
private Variables variables;
void Start()
{
variables = GameObject.Find("ScriptHolder").GetComponent<Variables>();
}
public void Mine()
{
variables.coins += variables.minePower;
}
}