Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Sami_Pal · Aug 29, 2016 at 01:39 PM · coroutinecolorarraysrandom.rangeloops

Make a GameObject changes between 2 colors with a time wait

Hey guys,

I'm making a simple 2D game, I'm stuck at this issue.

There's a sprite (square) and it moves horizontally in a fixed speed, but the square color is changing between Blue and Red, It start randomly with color lets say it Blue and after some adjustable time from me, ex (5 or 7 seconds) it changes to Red.. and so on.

I'm made an array of tow colors and I tired randomize the switching with Random.Rang but the it was too fast and it started like the square is blinking way too fast. Then I tried to use Coroutine to fix the blinking, but I couldn't apply it right.

If there's anyone can help me with this, I would be very thankful..!

Keep in mind, If this works, I'm gonna move to the next thing which will cause another problem:

While the square is moving, if it hits an object (ball or something similar) it will change the current color randomly, so for example, if the square is Red and it hits the ball it will change either to (Blue) or (Red)--which basically stays the same)

With this code, the squre is jst start with Blue and never changes, if you noticed in the Update method, it started with Red not Blue .... What's happening.

 public class MovingMagnet : MonoBehaviour {
 
     private Rigidbody2D myRigid;
     public float moveSpeed;
     private Collider2D myCollider;
     public GameObject hiddenBox;
     private FixedMagnetSpawner fixedMagnet;
     private SpriteRenderer myRender;
 
 
     Color[] colors = new Color[2]; //array of colors
 
     void Start ()
     {
         myRigid = FindObjectOfType<Rigidbody2D>();
         myCollider = GetComponent<Collider2D>();
         fixedMagnet = FindObjectOfType<FixedMagnetSpawner>();
         myRender = GetComponent<SpriteRenderer>();

         colors[0] = Color.red; 
         colors[1] = Color.blue;
     }
 
     void Update ()
         {
         myRigid.velocity = new Vector2(moveSpeed, myRigid.velocity.y);
        
             myRender.GetComponent<SpriteRenderer>().color = colors[0];
             //StartCoroutine("TimeWait", 5f);
         myRender.GetComponent<SpriteRenderer>().color = colors[1];
            StartCoroutine("TimeWait", 5f);
     }

     IEnumerator TimeWait(float waitingTime)
     {
         yield return new WaitForSeconds(waitingTime);
     }
  }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Cynikal · Aug 29, 2016 at 04:25 PM

     public Color lerpedColor = Color.white;
     void Update() {
         lerpedColor = Color.Lerp(Color.white, Color.black, Mathf.PingPong(Time.time, 1));
     }
 
Comment
Add comment · Show 3 · Share
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 Sami_Pal · Aug 29, 2016 at 05:18 PM 0
Share

Thanks a lot for answering, But It's not working, can you please explain more about this, I might understand it and try again.

avatar image Cynikal ♦ Sami_Pal · Aug 29, 2016 at 05:28 PM 0
Share

What exactly is not working? Is it not doing ANYTHING?

Did you assign the lerpedColor to the material?

(Also in Update)

myRender.GetComponent().color = lerpedColor;

avatar image Sami_Pal Cynikal ♦ · Aug 30, 2016 at 12:48 PM 0
Share

It works, one problem left !! here's what I got

https://i.imgflip.com/19qqx7.gif

The transition between the two colors is kinda in a gradient way and its too fast, I want the colors changes between Red and Blue only..without any gradient effect and kinda like a quick reverse but with adjustable duration like every 3 seconds it becomes Red and after another 3 secs it becomes Blue in an instant and loops like this. Any suggestion?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Deactivate GameObject after Coroutine ended 1 Answer

How to instantiate a random object from an array? 3 Answers

Randomly Select String from Array 0 Answers

How to make certain elements in an array rarer when using random selection? 4 Answers

How do you get different random numbers for each object array? 1 Answer


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