So I have most of the code completely working at this point. I am able to change a material with the button click. However, the problem arises when I try to change element 1 instead of element 0.
This is my current code which isn’t working.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ColourChangeShield : MonoBehaviour {
[SerializeField]
private Material[] materials;
private Renderer rend;
private int selectedMaterialIndex = 0;
void Start()
{
rend = GetComponent<Renderer>();
rend.enabled = true;
SelectMaterial(materials[selectedMaterialIndex]);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
SelectNextMaterial();
if (Input.GetKeyDown(KeyCode.Alpha2))
SelectPreviousMaterial();
}
public void SelectNextMaterial()
{
selectedMaterialIndex++;
if (selectedMaterialIndex >= materials.Length)
selectedMaterialIndex = 0;
Debug.Log("selectedMaterialIndex: " + selectedMaterialIndex);
SelectMaterial(materials[selectedMaterialIndex]);
}
public void SelectPreviousMaterial()
{
selectedMaterialIndex--;
if (selectedMaterialIndex < 0)
selectedMaterialIndex = materials.Length - 1;
Debug.Log("selectedMaterialIndex: " + selectedMaterialIndex);
SelectMaterial(materials[selectedMaterialIndex]);
}
public void SelectMaterial(Material material)
{
rend.materials[2] = material;
}
}
However, when the void SelectMaterial is structured like this it works.
public void SelectMaterial(Material material)
{
rend.sharedMaterial = material;
}
but only changes element 0 which is not the one I want to change. What can I do to make sure element 1, 2, 3 etc is changed.
Thank you in advance for any help you can provide