Question by
Tadbit · Sep 17, 2017 at 10:51 PM ·
c#scripting beginner
I'm trying to create a script to store a number of colors and when a gameObject is selected it would display a menu and user can choose what color to assign it.
Ultimately this will be used with VR and I know I'll need some sort of menu when it comes to this but right now I'm looking to write the script for just changing the color of then objects in the game. How can I store the colors that I want and when the user clicks on a certain object, the color will change? Right now I'm looking at this below as an example, so when the user clicks on the object it'll change with whatever materials are stored. What I want is to implement this for VR and how I should go on about that.
public class ColorChanging : MonoBehaviour {
public Material[] materials;//Allows input of material colors in a set size of array;
public Renderer Rend; // what's being rendered
private int index = 1;//Initialize at 1
// Use this for initialization
void Start () {
Rend = GetComponent<Renderer> ();//Gives functionality for the renderer
Rend.enabled = true;//Makes the rendered 3d object visable if enabled;
}
void OnMouseDown() {
if (materials.Length == 0)//If there are no materials nothing happens.
return;
if (Input.GetMouseButtonDown (0)) {
index += 1;//When mouse is pressed down we increment up to the next index location
if (index == materials.Length + 1)//When it reaches the end of the materials it starts over.
index = 1;
print (index);
Rend.sharedMaterial = materials [index - 1];
}
}
}
Comment