Simple material script mistake (help with C#)

Hello all,

I have waterfalls in my project which have 3 different planes of geometry, scrolling their UVs at different intervals, in order to achieve the effect I want. To do this I have been using the UV Scrolling code from the wiki, and I attached the script three times to each waterfall (one for each material index).

Here is the original code:

using UnityEngine;
using System.Collections;

public class uvScroll : MonoBehaviour
{
    public int materialIndex = 0;
    public Vector2 uvAnimationRate = new Vector2( 1.0f, 0.0f );
    public string textureName = "_MainTex";

    Vector2 uvOffset = Vector2.zero;

    void LateUpdate()
    {
        uvOffset += ( uvAnimationRate * Time.deltaTime );
        if( renderer.enabled )
        {
            renderer.materials[ materialIndex ].SetTextureOffset( textureName, uvOffset );
        }
    }
}

Desiring a clean project, I attempted to combine the functions of the three into one simple script, but my scripting skills are horribly lacking and the UVs end up being scrolled at a rate equal to the sum of all 3 floats, rather than each material index scrolling at its own rate.

Here is my modified code:

using UnityEngine;
using System.Collections;

public class uvScroll_waterfall : MonoBehaviour
{
    public int materialIndex0 = 0;
	public int materialIndex1 = 1;
	public int materialIndex2 = 2;
    public Vector2 uvAnimationRate0 = new Vector2( 0.0f, 1.25f );
	public Vector2 uvAnimationRate1 = new Vector2( -0.15f, 1.0f );
	public Vector2 uvAnimationRate2 = new Vector2( 0.15f, 0.75f );
	public string textureName0 = "_BumpMap";
    public string textureName1 = "_MainTex";
	public string textureName2 = "_MainTex";

    Vector2 uvOffset = Vector2.zero;

    void LateUpdate()
    {
        uvOffset += ( uvAnimationRate0 * Time.deltaTime );
        if( renderer.enabled )
        {
            renderer.materials[ materialIndex0 ].SetTextureOffset( textureName0, uvOffset );
        }

        uvOffset += ( uvAnimationRate1 * Time.deltaTime );
        if( renderer.enabled )
        {
            renderer.materials[ materialIndex1 ].SetTextureOffset( textureName1, uvOffset );
        }

		uvOffset += ( uvAnimationRate2 * Time.deltaTime );
        if( renderer.enabled )
        {
            renderer.materials[ materialIndex2 ].SetTextureOffset( textureName2, uvOffset );
        }
    }
}

Can a more experienced C# user tell me how to structure this so that each material scrolls at it’s own rate?

Many thanks for any help or suggestions!

The reason it’s doing so is because you’re executing the same thing three times in a row.
Any reason why you want to do that anyway?
According to what you said you have 3 different planes, which I assume are different gameobjects? If so, attach a script to each one and change attributes in that instance.

It’s actually one game object with 3 different planes, each having a unique material assignment.

Tell me more

The problem might be in

uvOffset += ( uvAnimationRate1 * Time.deltaTime );

What is done here as far as I understand is it adds more to the value each time thus speeding it up. Try using three different offsets instead of one.

Thanks for the reply! I will try this.