Error in script to swap textures at runtime

I am trying to swap the texture on a cube at runtime on mouse click. But I am getting errors (for lines in bold) in the following script

public class swapTexture : MonoBehaviour {
public Texture[ ] textures;
public int index;

void Start () {

}

void Update () {
if (Input.GetMouseButtonDown (0)) {
index++;
index%= textures.length;
renderer.material.mainTexture=textures[index]

}

}
}
I am getting following errors:
error CS0103 : The name ‘Texture’ does not exist in the current context
error CS0103 : The name ‘Input’ does not exist in the current context
error CS0103 : The name ‘renderer’ does not exist in the current context

What am I missing here?

Please use code tags. Its very hard to ready without.
You are missing a semi colon after [index]
renderer is obsolete so use GetComponent instead.

So something like this

using UnityEngine; //  Did you miss this line?

public class swapTexture : MonoBehaviour
{
    public Texture[] textures;
    public int index;
    private Renderer _renderer;

    void Start()
    {
        _renderer = GetComponent<Renderer>();
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            index++;
            index %= textures.Length;
            _renderer.material.mainTexture = textures[index];
        }
    }
}
1 Like

Thank you so much Karl .
Apologies for the bad formatting, will keep in mind.
Using GetComponent worked!

Thanks again!!

1 Like