SO i have a script for maintaining timer , but i want to access it from another script and add 2 seconds whenever “if”( condition is true) is used
THIS IS TIMER SCRIPT
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Timer : MonoBehaviour {
public float myTime = 30f;
// Update is called once per frame
void Update () {
GetComponent<Text>().text = (myTime -= Time.deltaTime).ToString("f0");
if(myTime<=0) {
Debug.Log("GAME OVER");
}
}
}
Now i have tried accessing it from this script , but it seems to restart my timer rather than adding 2 seconds
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class onClick1 : MonoBehaviour {
public Text ScoreText;
private int Count = 10;
private int wrongAnswer = -5;
public bool hello;
**private float RightAnswer = 2.0f;**
public void SayHello( bool hello = false ) {
if (GetComponent<Image> ().color == GameObject.FindGameObjectWithTag ("DisplayButton").GetComponent<Image> ().color) {
GameObject thePlayer = GameObject.FindGameObjectWithTag("GameController");
DisplayColor displayColor = thePlayer.GetComponentInChildren <DisplayColor>();
ChangeColor[] changeColors = thePlayer.GetComponentsInChildren <ChangeColor>();
foreach(ChangeColor changeColor in changeColors ) {
Debug.Log(changeColor .gameObject.name);
changeColor.ColorME();
}
**Timer timer = thePlayer.GetComponentInChildren<Timer>();**
hello = true;
displayColor.changeColor();
**timer.myTime =+ RightAnswer;**
ScoreText.text = "SCORE: " + Count;
} else if (GetComponent<Image> ().color != GameObject.FindGameObjectWithTag ("DisplayButton").GetComponent<Image> ().color) {
hello = false;
ScoreText.text = "SCORE: " + wrongAnswer;
}
}
}