- Home /
sync color change of objects c#
Hi, I'm very new to C# and am stuck trying to change the color of multiple gameobjects in syncronization.
Basically I have up to 20 lasers on the screen at any given moment which when effected by a buff they change color. When the buff duration is running low, I want all lasers in the scene to flicker back and forth from thier original color. I have tried 2 methods of doing so but both result in the lasers changing color individually instead of all in sync.
Here's the snippets of the code performing the color change. Any help would be greatly appreciated.
attempt 1:
void Update ()
{
if ((player.laserFlash) && (player.laserOddEven))
{
if (player.speedBoostStage == 1)
{
spriteRenderer.color = speedStage2Color;
}
if (player.speedBoostStage == 2)
{
spriteRenderer.color = speedStage3Color;
}
}
if ((player.laserFlash) && (!player.laserOddEven))
{
spriteRenderer.color = speedStage1Color;
}
}
Attempt 2 (same as above, but modified the action):
GameObject[] lasers = GameObject.FindGameObjectsWithTag("FriendlyProjectile");
foreach (var go in lasers)
{
spriteRenderer.color = speedStage1Color;
}
Answer by dan_wipf · Oct 28, 2018 at 02:01 PM
your second attempt was almost wright, you just didnt use go. try something like this:
GameObject[] lasers = GameObject.FindGameObjectsWithTag("FriendlyProjectile");
foreach (var go in lasers)
{
go.getcomponent<SpriteRenderer>().color = speedStage1Color;
}
Thanks for trying but that still gave me the same results.
The results exactly are it's like 1 laser is odd while the next is even, then odd, then even and so on. They don't all change to odd/even at the same time when bool laserOddEven flips.
The oddeven code called during the Update function is:
if (laserFlash) { laserOddEven = $$anonymous$$athf.FloorToInt(Time.time * 8) % 2 == 0; }
EDIT: actually the problem was here. It seems the speed it was flipping was too fast for the renderer. I changed Time.time to * 2 and it's all flashing in sync.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to initialise another 'game mode' on click? 1 Answer
How can I change a tile color in Unity by using C#., 2 Answers