Hello there, I would like to know how to set the color of an object (customize it) through a button. This object is in multiple scenes and I would like the button to set the color of it. I am new to coding and Unity, and this is confusing to me. I’m able to set the color of the object for one scene, but don’t know how to set it so it will be the same color of that object for the other scenes. If someone can provide me with some code or some help, it would be appreciated. (Specifically the object is a paper airplane)
Basically you need to persist the color data, that is you need to store it somehow. There are different approaches to doing this:
-
get the shared material of the object’s renderer, and modify the color of that (but you will lose the color after restarting the application (the Editor may keep it))
-
make the airplane object persistent with DontDestroyOnLoad() and never destroying it. This is also not recommended, because 1) you will still lose the color between runs 2) complicates the management of your paper airplane 3) DontDestroyOnLoad() will become deprecated functionality
-
this is the best, but the most complicated approach: you store the color somewhere, possibly in PlayerPrefs, and whenever you instantiate the paper airplane (or it could be enough at the start of a level) you load the color from the PlayerPrefs and apply it