vendredi 18 mai 2018

Unity can't access a random number from outside a function

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


public class GuessManagerScript : MonoBehaviour {

public InputField input;
public Text text;
int random = Random.Range(0, 1000);

public void onGuessEnter() {

    print(random);
    int inum = int.Parse(input.text);

    if (inum == random) 
        text.text = "Congrats, you guessed it!";
    else if (inum < random)
        text.text = "Bigger!";
    else if (inum > random) 
        text.text = "Smaller!";
   }
}

Hey everybody! Maybe it's a stupid question, but i couldn't solve it. I wanted to make a number guessing game where the computer guess your number and also you can guess it's number, but in the very finish, where i made a random number generator i stopped by a problem. I made my function named onGuessEnter which is called when the player hits enter after he entered his guess, and my problem is that if i run the game (Visual Studio doesnt make a problem about it) Unity stops because of an error that contains the following line: RandomRangeInt can only be called from the main thread. (If i put the random generator into the function, it generates a new random number every enter hit) Can someone help me to make the input field and the random generator working well together? (I mean it has to generate a random number only once and not always when the enter button is getting pressed and i could access that random number from onGuessEnter) Thanks for the answers, Kristóf.




Aucun commentaire:

Enregistrer un commentaire