Hi! I need help for something : I have a timer and I need to stop it when I finish the level. But I can only know if the level is finished by accessing the “finished” variable from my playerScript. The problem is : I have no idea of how to do it, especially for the part where I have to access the variable from another script. I have tried several techniques but unfortunately, I don’t know why but none of them works…( I’m with Unity 4.6)
Here is my code :
public class Timer : MonoBehaviour {
public float timer;
private GUIStyle guistyle = new GUIStyle();
// Use this for initialization
void Start () {
timer = Time.timeSinceLevelLoad;
}
// Update is called once per frame
void Update () {
timer += Time.deltaTime;
}
void OnGUI()
{
float minutes = Mathf.FloorToInt(timer / 60f);
float seconds = Mathf.FloorToInt(timer - minutes * 60);
string niceTime = string.Format("{0:0}:{1:00}" , minutes, seconds);
guistyle.fontSize = 20;
guistyle.normal.textColor = Color.white;
GUI.depth = 1;
GUI.Label(new Rect(10,34,500,200),"Time : "+ niceTime, guistyle);
}
}
public class PlayerScript : MonoBehaviour
{
public bool finished = false;
void OnTriggerEnter (Collider other)
{
if (count >=cubesNeeded && count < maxCubes)
{
WinText.text = "You Win!!!";
GameObject.FindWithTag("cotillons").GetComponent<ParticleSystem>().enableEmission = true;
finished=true;
}
}
}
If you want to access a variable or a method from another script, you need a reference to the gameobject - to which the script is attached. Then you should use GetComponent() on the gameobject to access the script.
bool isFinished = playerObject.GetComponent<PlayerScript>().finished;
This line will create a boolean variable, and access the PlayerScript class (which is attached to the playerObject as a script component) from which it will return the boolean called ‘finished’.
Note that there are several error possibilities in this line:
- The playerObject can not be null.
- The playerObject has to have a PlayerScript attached to it.
- The ‘finished’ variable has to be public.
Also, you should not use GetComponent frequently. If you need to access a script more times, you should create a variable which has a type of the script’s class name (in this case: PlayerScript) and fill that variable with a reference to the PlayerScript component. From there you can always access the script as long as it’s in the scene.
The last thing: in your example, you want to stop a timer when a boolean variable becomes true. Instead of checking it in the Update() function, you should rather use a different way. Create a new method which does two things: sets the ‘finished’ variable to true, and calls a ‘StopTimer()’ function, which should be a public function on the timer script. By using this, you are much more efficient and performance-friendly.
So here’s one really cool thing about unity. When you want to access things in another script, just define it like you would a variable!!
public class Timer : MonoBehavior {
//your other variables are here
public PlayerScript playerScript = new PlayerScript();
//your other code here
//to access finished just do this
if(playerScript.finished)
//do something cool!
HTH!