Ok, Code (csharp): #pragma strict //Rand is used to randomly select and object private var rand : int; //Time is the time between moving the blocks var time : float = 1; //Offset is how far the blocks are away from eachother var offset : float = 5; //Blocks var blockNormal : GameObject; var blockCollapse : GameObject; var blockRocks : GameObject; //Previous block position private var prevBlock : Transform; InvokeRepeating("Place",0,time); function Update() { //Get a random number between 1 and 3 (Inclusive) rand = Random.Range(1,4); //Debugging info Debug.Log(rand); } function Place() { //if rand is 1, then move blockNormal if(rand == 1) { blockNormal.transform.position.z = prevBlock.transform.position.z + offset; prevBlock.transform.position.z = blockNormal.transform.position.z; } else if(rand == 2) //If rand is 2, move blockCollapse { blockCollapse.transform.position.z = prevBlock.transform.position.z + offset; prevBlock.transform.position.z = blockCollapse.transform.position.z; } else if(rand == 3) //If rand is 3, move blockRocks { blockRocks.transform.position.z = prevBlock.transform.position.z + offset; prevBlock.transform.position.z = blockRocks.transform.position.z; } } The code should randomly select a block from the 3 GameObjects, and then place it in front of the previous block. However, it is not doing anything. Help? EDIT: The three game objects are already present in the scene, parented to an empty. That empty has this code.
2 Things: prevblock is private and not assigned; do this -_^ : Code (csharp): function Start() { prevBlock = blockNormal.transform; //maybe wrong InvokeRepeating("Place",0,time); }
Thanks. This sort of got it to work, Code (csharp): #pragma strict //Rand is used to randomly select and object var rand : int; //Time is the time between moving the blocks var time : float = 1; //Offset is how far the blocks are away from eachother var offset : float = 5; //Blocks var blockNormal : GameObject; var blockCollapse : GameObject; var blockRocks : GameObject; //Previous block position var prevBlock : Transform; InvokeRepeating("Place",0,time); function Start() { //Sets prevBlock to a location so we can start movement prevBlock = blockNormal.transform; //maybe wrong } function Update() { //Get a random number between 1 and 3 (Inclusive) rand = Random.Range(1,4); //Debugging info Debug.Log(rand); } function Place() { //if rand is 1, then move blockNormal if(rand == 1) { blockNormal.transform.position.z = prevBlock.transform.position.z + offset; prevBlock.transform.position.z = blockNormal.transform.position.z; } else if(rand == 2) //If rand is 2, move blockCollapse { blockCollapse.transform.position.z = prevBlock.transform.position.z + offset; prevBlock.transform.position.z = blockCollapse.transform.position.z; } else if(rand == 3) { blockRocks.transform.position.z = prevBlock.transform.position.z + offset; prevBlock.transform.position.z = blockRocks.transform.position.z; } } For some reason giant holes start appearing between objects. I understand why this is happening, (Because, for example, '1' is generated a couple of times in a row so that individual block just moves forward. How do I stop this?
Sorry for bump, but this is really annoying me. I cannot make it so that I can never get a consecutive number, because that would be way to difficult for the player -- so what would I do?
To get it so that its non consecutive just add a line after you roll for the random number to check if it was one higher than the last one. You'd need a variable for this as well. Code (csharp): // At the top // prevRand is used to hold the previous random number so that we can manage the order of blocks private var prevRand : int = 0; // starts as 0 so we don't get any weird errors, could be set in start/awake as well. // In your Update Function, replacing what you currently have // Check to make sure we don't have consecutive blocks, so 1 -> 2 is not okay, 2 -> 3 is not okay. do { rand = Random.Range(1,4); } while (rand != prevRand + 1); prevRand = rand; // Set up prevRand so we can check next update Should be noted that this can be weird and give you a lot of runs through the loop in a row which can slow you down a little bit. It's not a big deal, you won't be able to notice it likely and if you add more blocks it will repeat the loop less and less. Getting a random number takes no time at all, but I wouldn't do this kind of thing with something more taxing. This will allow 3 -> 1 as well, not sure if you want that or not but you can add an or statement in the while statement to fix it if you want. Make sense? This work for you?
You need to either not select a block that isn't last (meaning, always just bring the last block around to the front), or instantiate multiple copies and clear out blocks that trail out of view. Either way, using an array will absolutely make the logic simpler.