- Home /
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);
}
}
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));
}
Thanks a lot for answering, But It's not working, can you please explain more about this, I might understand it and try again.
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;
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
Follow this Question
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