[HELP] I want to make Materials switch back and fourth automatically looping

I have tried making police lights using actual light objects in my game with lens flares and they just won’t show up on a lighter background, so i created emission textures that look better. I’ve never ever done this before so I need 100% help, how do I make it switch materials consistently on an infinitely repeating pattern that works like police sirens?

By the sounds of it, you don’t need to swap out textures. I would think that toggling the emission state would give the effect you desire.

I need to swap emissions, one with red lights on and one with blue lights

I have a snippet that can give you an example on how to loop the emission property, but FYI it only glows, it doesn’t cast actual light, is that you want?

Ill post tge snippet when im home

yeah thats what I want, the emission on the material to loop between changing between emission 1 and emission 2 in a loop

        IEnumerator ChangeGlowStrip (Color[] color) { //takes in a color array to loop though

            // the "glowStrip" array is a Material array with all the materials that needs the emission value changed
            for (int i = 0; i < glowStrip.Length; i++)
                glowStrip [i].SetColor ("_EmissionColor", color [0]);
            int index = 0;//our current index
            float v = 0.0f;//how far are we into changing the color

            while (fightOver == false) {

                v += Time.deltaTime / 7; // divisor is how long it takes to change from one color to the next
                float s = Mathf.PingPong (v, .5f) * 2.0f; //this, v
                s = (1.0f - (1.0f - s) * (1.0f - s) * (1.0f - s) * (1.0f - s));//and this are just styling.
                for (int i = 0; i < glowStrip.Length; i++)
                    glowStrip [i].SetColor ("_EmissionColor", color [index] * s * 1.75f); //set each materials color to our current color by the index, multiply by s to get a curve and the 1.75f is pretty specific to my use case.
                if (v >= 1.0f) {//if v is bigger then 1, we've lerped though the color
                    v = 0.0f; //reset v
                    index = (index + 1) % color.Length; //increase index by one, modulo all the colors(to loop back when we're done)
                }
                yield return null;
            }

You’re code will probably be different but that should get you going.(if you wanna use this as-is feel free)

if you just wanna get it to change from one to the other you can change the color, yield for how ever long you want with WaitForSeconds, repeat.

also, as a side note, you could make the siren spin by giving the emission a circle texture that it can show though and just move it along the X axis

hmm, it seems that your code changes emission/material colour correct? I will try it, (maybe I’m just being stupid and the code you gave is exactly what i want) but I need the material to keep switching from material1 to material2 repeatedly forever on the object, I’ll send some screenshots of the same material except one has red light emission and the other has blue light emission.

4429768--404698--- (8).jpg
4429768--404701--- (9).jpg

Yes, it does change the emission color of the material, i though that’s what you wanted.

if you want to change the whole material just replace the ‘color’ array with a material array and the ‘glowStrip’ material array for a renderer array and assign renderer.material = material.

something like this

Renderer[] rend; //renderer attached to siren
float waitTime = .1f;
IEnumerator LoopMaterials(Material[] mat){

    WaitForSeconds wait = new WaitForSeconds (waitTime);
    while(true){

    int index = 0;

    for(int i = 0; i < rend.Length; i++)
        rend[i].material = mat[index];

    index = (index + 1) % mat.Length;
    yield return wait;
}

hmmm, tried many methods, still can’t get it, oh well.

Show us what you got, what did you try?

don’t just say “it didn’t work and give up”.