How to change car color with multiple buttons?
Note: This is my second day using Unity and I'm not familiar with C#.
Each color is a button using a background image.
How can I assign a color to each button and make them change the color of the car?
Answer by incorrect · Jan 05, 2016 at 12:05 AM
using UnityEngine;
using System.Collections;
public class ButtonColorChanger : MonoBehaviour
{
[SerializeField]
public Color color;
[SerializeField]
private GameObject targetObject;
public void SetColor()
{
if(targetObject == null)
{
Debug.LogError("Traget Object is not assigned!");
return;
}
Renderer objectRenderer = targetObject.GetComponent<Renderer>();
if(objectRenderer == null)
{
Debug.LogError("Traget Object has no renderer!");
return;
}
Material mat = objectRenderer.material;
mat.color = color;
}
}
Attach this script to each button, select your car model as target object and pick proper collors. Make buttons call SetColor() method of this script.
Keep in mind that you have to learn C# and Unity.
Thank you for the reply! Here's what I was able to figure out:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Caldera_Red : $$anonymous$$onoBehaviour {
public void onClick () {
GameObject Car = GameObject.Find ("Component2");
Car.GetComponent<Renderer>().shared$$anonymous$$aterial.color = new Color(183/255.0F, 33/255.0F, 62/255.0F, 255/255.0F);
}
}
That's almost the same solution I've suggested, but your will return 'Null Reference Exception' if there is no object called 'Component 2' in scene as well as it lacks ability to pick color via Inspector in Unity Editor.
Your answer

Follow this Question
Related Questions
How can i make a hold method for addforce? 0 Answers
Changing Color of Object Using a Timer 1 Answer
Assigning a GameObject variable to equal another GameObject variable via C# script. 0 Answers
Trying to make the Cursor Invisible when a UI button is pressed 2 Answers
Add an Icon next to a text inside a button Unity 5.6 1 Answer