- Home /
For Loop isn't working properly!
I've made a script such that it instantiates an object randomly choosing between 2 objects. But, now the For loop doesn't work. Here's the script:
var virusPrefab : Transform;
var filePrefab : Transform;
var position: Vector3;
var virusesKilled : int = 0;
var myResult : result;
var randomVirus : float;
var randomFiles : float;
var randomTotal : float;
var virusOrFile : float;
function Start(){
var myResultObject : GameObject = GameObject.FindGameObjectWithTag("Finish");
if (myResultObject != null) {
myResult = myResultObject.GetComponent(result);
}
randomTotal = Random.Range(45, 60);
randomVirus = randomTotal - 25;
randomFiles = randomTotal - randomVirus;
}
function Update(){
position = Vector3(Random.Range(0.5, 2), Random.Range(-2, 2), 6.351526);
myResult.total = randomTotal;
myResult.totalVirus = randomVirus;
myResult.score = virusesKilled;
myResult.totalFiles = randomFiles;
}
//The For loop section starts from here...
yield WaitForSeconds(3);
for (var i : int = 0;i < randomTotal; i++) {
virusOrFile = Random.Range(0, 1);//To choose between the 2 objects
if(virusOrFile == 0){
var file : Transform = Instantiate(filePrefab, position, Quaternion.identity);
file.transform.parent = GameObject.Find("files").transform;
}
if(virusOrFile == 1){
var virus : Transform = Instantiate(virusPrefab, position, Quaternion.identity);
virus.transform.parent = GameObject.Find("viruses").transform;
}
yield WaitForSeconds(1);
}
//And ends here
if(virusesKilled < 15){
myResult.enabled = true;
myResult.OnGUI();
Application.LoadLevel("gameOver");
}
function AddScore(score : int){
virusesKilled += score;
Debug.Log("Score is : " + virusesKilled);
return virusesKilled;
}
if(virusesKilled > 15){
myResult.enabled = true;
myResult.OnGUI();
}
I decided to add all of the script here, if you see any variable problem.
Before, I have written the loop like this:
yield WaitForSeconds(3);
for (var i : int = 0;i < randomTotal; i++) {
var virus : Transform = Instantiate(virusPrefab, position, Quaternion.identity);
virus.transform.parent = GameObject.Find("viruses").transform;
yield WaitForSeconds(1);
}
And it worked fine, but now after randomly choosing between two objects the loop isn't functioning!
Please help me...
robertbu is right. If you are using the 'int' version of random.range then the top number is never reached.
Check the page:
http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html
Answer by robertbu · Aug 14, 2013 at 04:01 PM
There are two forms of Random.Range(), one that takes integers, one that takes float. The integer version is exclusive of the top value. So 'Random.Range(0,1)' will only return 0. You need to use Random.Range(0,2) to get values 0 and 1.
You are unclear about what you mean by 'not working'. You should be executing the 'virusOrfile == 0' 'if' statement. If you are not, put Debug.Log() statements in the loop and inside each 'if' statement to see what is going on.
This code is also a good candidate for examining in the debugger. See this link for information on how to use the Mono debugger:
Thanks, robertbu! It worked! Thank you... thank you....thank you very much. You helped me get out of this big mess.
Your answer
Follow this Question
Related Questions
How to instantiate 2D object at fixed location? 1 Answer
removing an int from a Random.Range 1 Answer
Spawn at random? 1 Answer
instantiating at a random time 1 Answer
Scripts of instantiated objects 1 Answer