im trying to change the material of a plane at runtime based on a co routine it should be one material for 3 seconds and then the other for 3 seconds im trying to do something like this

public Material mat1;
public Material mat2;
bool material = false;

void Start () {
    current = this;
    GetComponent<Renderer>().material = mat1;
}

IEnumerator MyCoroutine()
{
    bubbles();
    yield return new WaitForSeconds(3.0f);
    clouds();
    yield return new WaitForSeconds(3.0f);
}


// Update is called once per frame
void Update () {
    StartCoroutine(MyCoroutine());
}
void bubbles()
{
    if (material == false)
    {
        GetComponent<Renderer>().material = mat2;
        go();
        material = true;
    }
}
void clouds()
{
    if (material == true)
    {
        GetComponent<Renderer>().material = mat1;
        go();
        material = false;
    }
}
public void go()
{
    pos += speed;
    if (pos > 1.0f)
        pos -= 1.0f;
    GetComponent<Renderer>().material.mainTextureOffset = new Vector2(pos, 0);
}

im kinda new to unity and coding really but in my eyes this should work its meant to have a parallax effect which does work, and it does change material but the material kinda flickers and stutters back and forth is there something im not accounting for?
also while im asking is there any way to parallax a sprite? not in the same way obviously as im using texture offset right now
thanks for any and all suggestions

if GetComponent().material = mat1 does not work try GetComponent().sharedmaterial= mat1;

try GetComponent().sharedMaterial= mat2;

try renderer.sharedMaterial instead of renderer.material
hope it help

Your issue is you are starting a new coroutine every frame in the Update() function. In this case you only want one coroutine.

Try moving the StartCoroutine(MyCoroutine()); call to your Start() function then either put a while loop in your coroutine or have it call itself at the end.