- Home /
Start countDownTimer (displayed on UI Text) when touching a button in VR ,Starting a countDown Timer when touching a button in VR
Hey everybody,
I am quite new to Unity and I am currently working on my first project. I am doing a VR project using the HTC Vive. In this project I have a Button and when I touch it with the Controller in my game I want that object to appear and at the same time I want a countDown Timer to start. It is working that the Object appears and the countdown Timer as a separate component is working as well but the timer is already starting when I press play and I only want it to start when the button is touched. For the timer I created a Canvas and an UI Text and this UIText holds the functionality of the countDownTimer.
The Button has a script showObject that holds the functionality of making the object appear and is using a OnTriggerEnter method.
How can I now do it that I call the countDowntimer script within the showObject script? Or how can I combine the scripts if that's easier.
I already tried to call the countDownTimer Script with adding public countDownTimer CountDownTimerObj = null; to the showobjmct Script and then calling CountDownTimerObj.Update(); in the onTriggerEnter method in my showObject Script but that didn't work yet...
I don't know if it helps but here is also my code from my countDownTimer:
public float timeValue = 90;
public Text timerText;
public void Update()
{
if (timeValue > 0)
{
timeValue -= Time.deltaTime;
}
{
timeValue = 0;
}
DisplayTime(timeLeft);
}
void DisplayTime(float timeToDisplay)
{
if(timeToDisplay < 0)
{
timeToDisplay = 0;
}
float minutes = Mathf.FloorToInt(timeToDisplay / 60);
float seconds = Mathf.FloorToInt(timeToDisplay % 60);
timerText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
}
And this is my showObjectScript (it is doing some more things than just showing an object but that doesn't really matter for this question that's why I didn't mention it). Also I only included the OnTriggerEnterMethod otherwise it is too much I think.
public GameObject Button;
public GameObject Objekt1;
public GameObject Objekt2;
....
private int mynum;
private int touched = 0;
public bool Objekt1done;
public bool Objekt2done;
.....
private List<int> usableObjects()
{
....
return ObjNums;
}
private GameObject PickRandomObject()
{
..... // picks one object out of a switch statement and returns it
}
}
void OnTriggerEnter(Collider col)
{
if (touched == 0 || touched == 2)
{
GameObject myObject = PickRandomObject();
myObject.SetActive(true);
Button.SetActive(false);
touched += 1;
StartCoroutine(WaitThenDie())
}
}
void ResetButton()
{
Button.SetActive(true);
touched += 1;
}
IEnumerator WaitThenDie()
{
....
}
ResetButton();
}
}
I appreciate any help. Thank you so much in advance!!
Best regards, Sarah ,Hey everybody,
I am quite new to Unity and I am currently working on my first project. I am doing a VR project using the HTC Vive. In this project I have a Button and when I touch it with the Controller in my game I want that object to appear and at the same time I want a countDown Timer to start. It is working that the Object appears and the countdown Timer as a separate component is working as well but the timer is already starting when I press play and I only want it to start when the button is touched. For the timer I created a Canvas and an UI Text and this UIText holds the functionality of the countDownTimer.
The Button has a script showObject that holds the functionality of making the object appear and is using a OnTriggerEnter method.
How can I now do it that I call the countDowntimer script within the showObject script? Or how can I combine the scripts if that's easier.
I already tried to call the countDownTimer Script with adding public countDownTimer CountDownTimerObj = null; to the showobjmct Script and then calling CountDownTimerObj.Update(); in the onTriggerEnter method in my showObject Script but that didn't work yet...
I don't know if it helps but here is also my code from my countDownTimer:
public float timeValue = 90;
public Text timerText;
public void Update()
{
if (timeValue > 0)
{
timeValue -= Time.deltaTime;
}
{
timeValue = 0;
}
DisplayTime(timeLeft);
}
void DisplayTime(float timeToDisplay)
{
if(timeToDisplay < 0)
{
timeToDisplay = 0;
}
float minutes = Mathf.FloorToInt(timeToDisplay / 60);
float seconds = Mathf.FloorToInt(timeToDisplay % 60);
timerText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
}
And this is my showObjectScript (it is doing some more things than just showing an object but that doesn't really matter for this question that's why I didn't mention it). Also I only included the OnTriggerEnterMethod otherwise it is too much I think.
public GameObject Button;
public GameObject Objekt1;
public GameObject Objekt2;
....
private int mynum;
private int touched = 0;
public bool Objekt1done;
public bool Objekt2done;
.....
private List<int> usableObjects()
{
....
return ObjNums;
}
private GameObject PickRandomObject()
{
..... // picks one object out of a switch statement and returns it
}
}
void OnTriggerEnter(Collider col)
{
if (touched == 0 || touched == 2)
{
GameObject myObject = PickRandomObject();
myObject.SetActive(true);
Button.SetActive(false);
touched += 1;
StartCoroutine(WaitThenDie())
}
}
void ResetButton()
{
Button.SetActive(true);
touched += 1;
}
IEnumerator WaitThenDie()
{
....
}
ResetButton();
}
}
I appreciate any help! Thank you so much in advance!!
Best regards, Sarah
Your answer
Follow this Question
Related Questions
Objects and parentobj connection 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Why don't the thumbsticks on my Oculus Quest 2 controllers give output? 1 Answer
Oculus OVR 0 Answers