- Home /
How to change color of the car in scene02 on button click of scene01? how I should use 'static' keyword.
(new to unity) My scene_01 have four buttons for four different colors, I want to change the color of the car happens in scene_02 with respect to selected color. I don't really know how to write script for accessing car's body material from scene_01.
Answer by Legend_Bacon · Dec 20, 2017 at 05:31 PM
Hello there,
You have several options there. Either:
Make a Data Holder object with DontDestroyOnLoad, or
Use the player prefs (or any kind of data saving, really) to get values from/to different scenes. For a color, you could save a Hex value as a string
PlayerPrefs.SetString("CarColor", myHexColorAsString);
. To load it, you could then usestring colAsString = PlayerPrefs.GetString("CarColor")
. And finally, to parse it follow this link: Hex Colors in Unity.
I hope that helps!
~LegendBacon
I just noticed the last part of the question, about the material.
To access a GameObject's material and change its color, you can go:
myGameObject.GetComponent<Renderer>().material.color = myColor;
Answer by Prastiwar · Dec 20, 2017 at 04:14 PM
You just need reference to car's material that you have in folder structure, so you can change it's color easily. Example:
[SerializeField] Material carMaterial;
void OnRedButton()
{
carMaterial.color = Color.red;
}
appreciated but the problem I am facing is to access the GameObject of another scene on button click from different scene.
Okey, to change car's material you don't have to access to car. $$anonymous$$aterials can be accessed from everywhere, so I don't really understand your problem. $$anonymous$$aybe this can help you: if you want access to car in scene_02 (I guess it's main game scene) you can make reference in scene_01 with, DontDetroyOnLoad, set $$anonymous$$aterial Color and then in scene_02 instantiate car from this specific reference. Also you can check Legend_Bacon's solution with PlayerPrefs