- Home /
One GameObject, two scenes
I'm currently working on a 2d platform bike based game. I've created a customise scene where you can change the colour of the bike, but when you hit play and the main scene loads, the colour changes don't appear. Is there a way to have one gameObject in two scenes, so the changes from one scene affect the other? I'm working in C# btw
Thanks
Answer by nsxdavid · Mar 16, 2013 at 03:48 PM
There are different ways you can achieve this. Two of the most common:
1) Have the object created in only one scene (like the initial scene) and mark it so that it will not be destroyed automatically when loading a new scene.
Example:
public class example : MonoBehaviour {
void Awake() {
DontDestroyOnLoad(transform.gameObject);
}
}
2) Make the bike a prefab. Then instantiate a GameObject from that prefab in each scene when it starts up. Then you can modify the prefab (like the color) and the copies that are created at runtime will have that color regardless of what scene that is done in.
Answer by Cyberpie_n_milk · Mar 16, 2013 at 04:39 PM
You can use DontDestroyOnLoad to insert the bike from the customize scene to the main scene, when you load it.
nsxdavid im trying to instantiate the gameObject, but I dont quite understand how it works. Coud you please explain? The reason why I cant use DontDestroyOnLoad is because in the 1st scene the bike is rotating still, and in the main game it'll be moving on the x axis