2

I am new to Unity and C#.

I am trying to make a simple program which will execute in console it will tell the user to wait for a certain time after the user click SPACE the computer will say how much he waited.

SO it's a simple code.

For doing this I have to generate a random number and some keyboard input is required.

But when I enter spacebar nothing happens.

NO COMPILATION ERROR.

Random Number generates number 0 only.

CODE:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class droid : MonoBehaviour {
    float startTime;
    float playerTime;
    float targetTime;

    // Use this for initialization
    void Start () {
            target();
    }

    // Update is called once per frame
    void Update () {
        playerTime = Time.time - startTime;

        if (Input.GetKeyDown(KeyCode.Space))
        {
            print("You took " + playerTime);
        }
    }

    void target() {
            print("Your Time " + targetTime);
            targetTime = Random.Range(0, 10);
            startTime = Time.time;
    }
}

2 Answers 2

4

You are printing the value before it's generated. In your target(), move this line:

targetTime = Random.Range(0, 10);

above

print("Your Time " + targetTime);
Sign up to request clarification or add additional context in comments.

Comments

0

For the random

    int i= Random.Range(minIntValue,maxIntValue)
    float j = Random.Range(minFloatValue,maxFloatValue)

For the times you can always use IEnumerators.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.