user5589852
user5589852

Reputation:

How to get the random number in C# and why keyboard input is not working?

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;
    }
}

Upvotes: 2

Views: 253

Answers (2)

cezikmertcan
cezikmertcan

Reputation: 44

For the random

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

For the times you can always use IEnumerators.

Upvotes: 0

SurvivalMachine
SurvivalMachine

Reputation: 8356

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);

Upvotes: 4

Related Questions