x


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);



        }


}
more ▼

asked Dec 23 '10 at 12:51 AM

RadiantLWguy gravatar image

RadiantLWguy
11 3 3 7

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.)

Dec 23 '10 at 05:54 AM yoyo

does the coroutines run at the same item as Update?

Dec 23 '10 at 05:44 PM RadiantLWguy

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 programing. THanks for your help.

Dec 26 '10 at 04:48 PM RadiantLWguy
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1680
x507
x296
x170

asked: Dec 23 '10 at 12:51 AM

Seen: 1211 times

Last Updated: Dec 26 '10 at 04:47 PM