- Home /
Instantiate color change loop
Hey all,
I created a script that would shoot cubes into the scene. I have it Instantiate a prefab. On a button press I want the cubes to incrementally change color. I have a script that works good for creating cubes and assigning a name to them.
I seem to be having troubles with the color change script. The script will change the first block. then change all the cubes after that at the same time. I have noticed that the Rscore print out in console will continue to decrease into like -550. When i pull the Waitforseconds out, the Rscore decreases like it should and all the cubes change at the same time. I have this code in a subroutine.
I am kinda new to unity and to coding. Basically I want select a cube by its name, change its color, wait, then repeat. If anyone can point out what i did wrong or if there is an easier way to do what I am wanting it would be much appreciated.
Function Update () {
Bcubes.renderer.material.color = Bcolor;
Rcubes.renderer.material.color = Rcolor;
Gcubes.renderer.material.color = Gcolor;
//Increases Teams score by 1 each button press
if(!Input.GetButton("Fire5") && Input.GetButtonDown(shootButB) && !equal)
{
Bscore++;
}
if(!Input.GetButton("Fire5") && Input.GetButtonDown(shootButR) && !equal)
{
Rscore++;
}
if(!Input.GetButton("Fire5") && Input.GetButtonDown(shootButG) && !equal)
{
Gscore++;
}
// Decreases Teams Score by 1 and destroys a cube each button press.
if(Input.GetButton("Fire5") && Input.GetButtonDown("Fire1") && !equal)
{
var DestroyCubeB : GameObject = GameObject.Find("Blue" + Bc);
if (DestroyCubeB)
{
Destroy (DestroyCubeB);
Bc++;
Bscore--;
}
}
if(Input.GetButton("Fire5") && Input.GetButtonDown("Fire2") && !equal)
{
var DestroyCubeR : GameObject = GameObject.Find("Red" + Rc);
if (DestroyCubeR)
{
Destroy (DestroyCubeR);
Rc++;
Rscore--;
}
}
if(Input.GetButton("Fire5") && Input.GetButtonDown("Fire3") && !equal)
{
var DestroyCubeG : GameObject = GameObject.Find("Green" + Gc);
if (DestroyCubeG)
{
Destroy (DestroyCubeG);
Gc++;
Gscore--;
}
}
//Checks if calculate is pressed
if (Input.GetButtonDown("Jump"))
{
equal = true;
}
//Compare scores and move Winners object
if((Bscore > Rscore && Bscore > Gscore) && equal)
{
if(Bwin.transform.position.y < Hmax && equal)
{
Bwin.transform.Translate(Vector3(0,1,0) * Time.deltaTime * speedUp);
}
hold();
}
if(Rscore > Bscore && Rscore > Gscore)
{
if(Rwin.transform.position.y < Hmax && equal)
{
Rwin.transform.Translate(Vector3(0,1,0) * Time.deltaTime * speedUp);
}
}
if(Gscore > Bscore && Gscore > Rscore)
{
if(Gwin.transform.position.y < Hmax && equal)
{
Gwin.transform.Translate(Vector3(0,1,0) * Time.deltaTime * speedUp);
}
}
}
function hold() {
if (Rscore > 0)
{
var Rchange : GameObject = GameObject.Find("Red" + Rc);
Rchange.renderer.material.color = Color.Lerp (Rcolor, Bcolor, Time.time / FadeSpeed);
yield WaitForSeconds(5);
Rc++;
Rscore --;
print(Rscore);
}
}
Can you edit the question to include the full subroutine, and where/how you call it? The code you've shown should never print Rscore < 0, unless something else is decrementing Rscore during the 5 second wait. But I presume you are starting this as a coroutine after launching each box, in which case there are many instances of the coroutine all decrementing Rscore. Perhaps it would work better if Rchange was passed as a parameter to the coroutine. (But without seeing more of your code I'm just guessing.)
Updated the code. I have another script that creates the cubes. This one does all the score keeping. Like I said, i am still really new when it comes to progra$$anonymous$$g. THanks for your help.
Your answer
Follow this Question
Related Questions
Simple rotation script. (loop) 3 Answers
How to compare and detect overlapping gameObject Prefabs using maths 0 Answers
How Instantiate Object Inside of Object and Instantiate Parent Object On Looping. 0 Answers
Checking if object intersects? 1 Answer
Instantiate an object as soon as another object is destroyed 2 Answers