Need Help! Unity2D Reaction Test!!!

Updated 2 days ago in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on February 3, 2019

Hello, I have a problem! I am trying to make a Reaction Test with Unity. It isn’t working. Could you PLEASE look at the scripts and look if there are any Errors?! In a random time between 1 and 10 Seconds an one-colored image appears and you have to press any key as fast as you can. An invisible timer is measuring the time between the moment when the Image appears and the moment when you press the key. The time shows up later. But this all isn’t working! Thank you!!! 

 

Here are my 2 scripts:

 

 
using UnityEngine;
using UnityEngine.UI;
 
public class TestScript : MonoBehaviour {
 
public GameObject colourImage;
 
public Text timerText;
 
public float timer;
 public float timerToColour;
 [SerializeField]
 private float randomNumber;
 
public bool timerActive = false;
 public bool ready = false;
 
public DisappearScript diesappearScript;
 
void Start()
 {
 randomNumber = Random.Range(1, 10);
 colourImage.SetActive(false);
 }
 
void Update () {
 if (ready == true)
 {
 timerToColour = timerToColour + Time.deltaTime;
 if (timerToColour == randomNumber)
 {
 colourImage.SetActive(true);
 timerActive = true;
 }
 }
 if (timerActive == true)
 {
 timer = timer + Time.deltaTime;
 timerText.text = timer.ToString();
 }
 
if (colourImage == true && Input.anyKeyDown)
 {
 timerActive = false;
 }
 }
 
public void Ready()
 {
 ready = true;
 }
}
 

 

This is the other script:

 

 
using UnityEngine;
 
public class DisappearScript : MonoBehaviour {
 
public GameObject text;
 public GameObject text2;
 public GameObject button;
 public GameObject ScriptManager;
 public GameObject timerText;
 
public TestScript testScript;
 
void Start () {
 text.SetActive(true);
 button.SetActive(true);
 timerText.SetActive(false);
 text2.SetActive(false);
 ScriptManager.GetComponent().enabled = false;
 }
 
public void OnButtonClick () {
 text.SetActive(false);
 button.SetActive(false);
 ScriptManager.GetComponent().enabled = true;
 testScript.Ready();
 }
 
public void Pressed()
 {
 text2.SetActive(true);
 timerText.SetActive(true);
 }
}
 
  • Liked by
  • FaZeBook
Reply
1 on February 3, 2019

It’s probably because you’re using == with floating point values. It’s almost impossible to get 2 floats to be exactly equal to each other when using time.

Instead of:

if (timerToColour == randomNumber)

 

try:

if (timerToColour >= randomNumber)
2 days ago

Yes, I fixed this already! But thank you very much! Now, everything is working, except of the timer. If I press any button, the timer doesn’t stop! But everything except of this works. Please help me, it’s very important!!!

Here are my scripts:

The new “Test Script” (this is the main script)

using UnityEngine;
using UnityEngine.UI;
public class TestScript : MonoBehaviour {
public GameObject colourImage;
public Text timerText;
public float timer;
 public float timerToColour;
 [SerializeField]
 private float randomNumber;
public bool timerActive;
 public bool ready = false;
 public bool pressedBool = false;
 public bool alreadyPressed;
public DisappearScript disappearScript;
void Start()
 {
 randomNumber = Random.Range(1, 10);
 colourImage.SetActive(false);
 alreadyPressed = false;
 timerActive = false;
 }
void Update () {
 if (ready == true)
 {
 timerToColour = timerToColour + Time.deltaTime;
 if (timerToColour >= randomNumber)
 {
 colourImage.SetActive(true);
 alreadyPressed = true;
 timerActive = true;
 }
 }
 if (timerActive == true)
 {
 timer = timer + Time.deltaTime;
 timerText.text = timer.ToString();
 }
if (colourImage != null && Input.anyKeyDown && alreadyPressed == true)
 {
 timerActive = false;
 pressedBool = true;
 alreadyPressed = false;
 }
if (pressedBool == true)
 {
 disappearScript.Pressed();
 }
 }
public void Ready()
 {
 ready = true;
 }
}

And the “DisappearScript”:

using UnityEngine;
public class DisappearScript : MonoBehaviour {
public GameObject beschreibung1;
 public GameObject text2;
 public GameObject button;
 public GameObject nextButton;
 public GameObject ScriptManager;
 public GameObject timerText;
public TestScript testScript;
void Start () {
 beschreibung1.SetActive(true);
 button.SetActive(true);
 nextButton.SetActive(false);
 timerText.SetActive(false);
 text2.SetActive(false);
 ScriptManager.GetComponent<TestScript>().enabled = false;
 }
public void OnButtonClick () {
 beschreibung1.SetActive(false);
 button.SetActive(false);
 ScriptManager.GetComponent<TestScript>().enabled = true;
 testScript.Ready();
 }
public void Pressed()
 {
 text2.SetActive(true);
 timerText.SetActive(true);
 nextButton.SetActive(true);
 }
}
Show more replies
  • Liked by
Reply
Cancel