- Home /
Change Objects colour with c# script
What I'm trying to do is let the player shoot three switches that will change the colour of another object (called crystal). I'm not sure how to change the colour of the crstal though. This is the script I've got so far (it compiles fine):
using UnityEngine; using System.Collections;
public class ColScript : MonoBehaviour { //Setting variables to work out which switches have been hit bool RedCrystal = false; bool BlueCrystal = false; bool GreenCrystal = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter(Collision col){
//Sees if red switch has been hit
if(col.gameObject.name=="RedSwitch1") //Checks to see if red switch has been colided with
{
//Turns red on if it's off and vice versa
if (RedCrystal == false){
RedCrystal =true;
}
if (RedCrystal ==true ){
RedCrystal =false;
}
}
//Does the same for green
if(col.gameObject.name=="GreenSwitch1")
{
if (GreenCrystal == false){
GreenCrystal =true;
}
if (GreenCrystal ==true ){
GreenCrystal =false;
}
}
//Does the same for blue
if(col.gameObject.name=="BlueSwitch1")
{
if (BlueCrystal == false){
BlueCrystal =true;
}
if (BlueCrystal ==true ){
BlueCrystal =false;
}
}
//Checks to see which switches are on and changes crystal matirial's colour appropriatly based on light mixing
if(RedCrystal==true & BlueCrystal==true & GreenCrystal ==true)
{
gameObject.renderer.material.color = Color.white;
}
if(RedCrystal==true & BlueCrystal==true & GreenCrystal ==false)
{
gameObject.renderer.material.color = Color.magenta;
}
if(RedCrystal==false & BlueCrystal==true & GreenCrystal ==true)
{
gameObject.renderer.material.color = Color.cyan;
}
if(RedCrystal==true & BlueCrystal==false & GreenCrystal ==true)
{
gameObject.renderer.material.color = Color.yellow;
}
if(RedCrystal==true & BlueCrystal==false & GreenCrystal ==false)
{
gameObject.renderer.material.color = Color.red;
}
if(RedCrystal==false & BlueCrystal==true & GreenCrystal ==false)
{
gameObject.renderer.material.color = Color.blue;
}
if(RedCrystal==false & BlueCrystal==false & GreenCrystal ==true)
{
gameObject.renderer.material.color = Color.green;
}
if(RedCrystal==false & BlueCrystal==false & GreenCrystal ==false)
{
gameObject.renderer.material.color = Color.black;
}
}
}
Any help is welcome
Comment