Hey, I am making a security code for a door and the code is working ok so far. The player has 10 seconds to punch in the correct code. If the code is punched in correct then the door opens. This all works but only because I know the secret code.
So, what I want to do is declare a variable of type int(var secretCode :int), and set its value to random.range(100,999) that way the security code is different each time you encounter this lock. The players only hint at the secret code is a onscreen GUIText that displays 3 scrambled numbers (GUIText). These numbers represent the actual code but are scrambled.
Example would be actual code = 321 and code hint = 213…
The player has to get the code correct within the 10 seconds or whatever number is set in the final version. As of right now I have the number reseting after it is correct.
Is there a way to do this using just Random.Range? My issue is how would I set the number to 321 and have the hint scrambled. Here is my code so far.
#pragma strict
//timer variables attach to game manager and drag GUI text into TimerText slot in inspector
var timer = 0.0;
var startTime = 0.0;
var maxTime = 20.0;
var timerText : GUIText;
//secret code variables
var secretCode : String;
var playerCode : String;
var newCode : int;
//place to enter playerCode
var text : String;
var done : boolean = false;
var codeHint : GUIText;
//score variables
var score = 0;
var scoreGUI : GUIText;
function Start ()
{
newCode = Random.Range(100,999);
secretCode = ""+newCode;
codeHint.text = ""+secretCode;
}
function Update ()
{
timer += Time.deltaTime;
Timer ();
HitDone();
}
function Timer ()
{
if(timer > maxTime)
{
timer = startTime;
}
}
function OnGUI()
{
if(done)
{
text = GUI.TextField (new Rect(500,25,100,30), text, 3);
GUI.Label (Rect (10, 10, 100, 20), "You Entered " + text );
timerText.text = ""+timer;
scoreGUI.text = ""+score;
}
else
{
GUI.Label (Rect (10, 10, 100, 20), "Enter Number");
text = GUI.TextField (new Rect(500,25,100,30), text, 3);
}
}
function HitDone()
{
if(Input.GetKeyDown("space"))
{
done = true;
CheckCode();
}
}
function CheckCode()
{
if(text == secretCode)
{
score+=100;
var newCode2 = Random.Range(100,999);
text = "";
Reset();
}
else
{
score = score;
}
}
function Reset()
{
newCode = Random.Range(100,999);
secretCode = ""+newCode;
codeHint.text = ""+secretCode;
}