- Home /
 
change color object
hello i want to change the color of an object if i press an key
example: green (press a) becomes red red(press q) becomes back green
i hope someone can help me :)
Answer by $$anonymous$$ · Apr 03, 2013 at 08:18 PM
Try these two links Input.GetKeyDown and Material.color.
Answer by izhankhalib · Apr 29, 2018 at 12:25 PM
This script you need to link with your GameObject ...
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class NewBehaviourScript : MonoBehaviour { public GameObject gameObject;
 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {
     if (Input.GetKeyDown(KeyCode.R))
     {
         gameObject.GetComponent<Renderer>().material.color = Color.red;
     }
     if (Input.GetKeyDown(KeyCode.B))
     {
         gameObject.GetComponent<Renderer>().material.color = Color.blue;
     }
     if (Input.GetKeyDown(KeyCode.G))
     {
         gameObject.GetComponent<Renderer>().material.color = Color.green;
     }
     if (Input.GetKeyDown(KeyCode.M))
     {
         gameObject.GetComponent<Renderer>().material.color = Color.magenta;
     }
 }
 
               }
Your answer
 
             Follow this Question
Related Questions
Referencing color change when object is 'deselected' 2 Answers
Changing a second objects material on trigger 0 Answers
Is changing the color of GUIText unavailable in the Free Edition? 1 Answer
Changing properties of sections of objects and take properties of objects during play? 1 Answer
On Touch change color 2 Answers