- Home /
Use A button to set a color using playerprefs in a different scene?
How could I use playerprefs to toggle a color of a gameobject in a different scene? I say I have 3 buttons on my menu scene. One changes my gameobject in the game scene to red, one to orange, and one to yellow. If one is pressed it deselects the one that was currently set. If one selected is clicked again, the default color would take place. I am guessing I would have to use playerprefs to start but don't know how and how to do it between scenes. If anyone could provide a code sample or lead me in the right direction I would be very appreciative. JavaScript would be preferred. Thanks.
Answer by Raynoko · Mar 13, 2015 at 01:29 PM
Try to create empty GO, and put this script him, and add: DontDestroyOnLoad() then, this empty GO stay always in any one scene, and in this script u can save this value (color, pos, atc.) And then u can change color to second gameobject in second scene. Hope helpful
Answer by Arishtanemi1998 · Mar 13, 2015 at 02:04 PM
I do not recommend using playerprefs as they cause hiccups in ur gameplay.you can create an empty gameobject instead of using player prefs and use singletons in it so multiple instances are not present.assign this script to the object
private static MyUnitySingleton instance = null;
public static MyUnitySingleton Instance {
get { return instance; }
}
void Awake() {
if (instance != null && instance != this) {
Destroy(this.gameObject);
return;
} else {
instance = this;
}
DontDestroyOnLoad(this.gameObject);
}
then add your color preference on another function.