I’m trying to set up a skin selector for my character. I want to be able to set up a manual method for selecting between multiple skins (i.e. a hotkey, a GUI Button, etc.) For some reason, I just can’t figure it out. I’ve been trying for about an hour and can’t seem to nail it down. Here’s what I have. I’m sure it’s a simple answer.
Note: I started with the Renderer.material script in the API
Sorry, I’m doing a terrible job of explaining it.
I am loading materials up in the Materials in the inspector. At the start of the game, I want players to be able to choose the skin they want and have that persist throughout the game.
I’m trying to find an efficient way to allow players to select the skin they want using a GetButton command. I’m having a hard time using a get button command to cycle between the materials loaded in the inspector.
public class SkinSelect : MonoBehaviour {
public Material[] materials;
public Renderer rend;
public GameObject player;
public int skin;
void Start() {
skin = 0;
player = GameObject.FindGameObjectWithTag ("Player");
rend = player.GetComponent<Renderer>();
rend.enabled = true;
}
void Update() {
if (Input.GetButton ("C")) {
*What goes here?*
}
if (materials.Length == 0)
return;
}
}
Right now, I’m using the script below. It works, but I feel like it’s really sloppy.
public class SkinSelect : MonoBehaviour {
public Material material1;
public Material material2;
public Material material3;
public Material material4;
public Material material5;
public Renderer rend;
public GameObject player;
public int skin;
void Start() {
skin = 1;
player = GameObject.FindGameObjectWithTag ("Player");
rend = player.GetComponent<Renderer>();
rend.enabled = true;
}
void Update() {
if (Input.GetButtonDown ("C")) {
skin = skin +1;
ChangeSkin();
}
if (skin >= 6) {
skin = 1;
ChangeSkin ();
}
DontDestroyOnLoad (this);
}
void ChangeSkin()
{
if (skin == 1)
{
rend.material = material1;
}
if (skin == 2)
{
rend.material = material2;
}
if (skin == 3)
{
rend.material = material3;
}
if (skin == 4)
{
rend.material = material4;
}
if (skin == 5)
{
rend.material = material5;
}
}
}