Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by RadiantLWguy · Dec 23, 2010 at 12:51 AM · instantiatecolorloopwaitforseconds

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



     }


}

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image yoyo · Dec 23, 2010 at 05:54 AM 0
Share

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

avatar image RadiantLWguy · Dec 23, 2010 at 05:44 PM 0
Share

does the coroutines run at the same item as Update?

avatar image RadiantLWguy · Dec 26, 2010 at 04:48 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges