Quantcast
Channel: Questions in topic: "dontdestroyonload"
Viewing all articles
Browse latest Browse all 416

How to make a persistent player a child in a new Scene?

$
0
0
Hi community, I'm new to Unity and have a problem right now. Also English isn't my native language, so please bare with me. I made my player object a singleton script, so I can keep it througout my levels. Via `Application.LoadLevelAsync` I load my new scenes from a Hub world. In one of the levels I want to make the player a child of another gameobject, but the player simply vanishes from the hierachy without an exception, it is just gone. **Singleton script for the player** public class MakePlayerPersistent : MonoBehaviour { private static MakePlayerPersistent privatePlayerInstance; public static MakePlayerPersistent publicPlayerInstance { get { if(privatePlayerInstance == null) { privatePlayerInstance = GameObject.FindObjectOfType(); DontDestroyOnLoad(privatePlayerInstance.gameObject); } return privatePlayerInstance; } } void Awake() { if(privatePlayerInstance == null) { privatePlayerInstance = this; DontDestroyOnLoad(this); } else { if(this != privatePlayerInstance) { print ("Destroy on Awake"); Destroy(this.gameObject); } } } void OnApplicationQuit() { print ("Destroy on Quit"); Destroy(this.gameObject); } } **Script, which adjust the player position in the new scene (which works) and parents the player (which doesn't work)** public class PlayerTransform : MonoBehaviour { bool parenting = false; bool playerSetter = false; void Update() { if(GameObject.Find("Player") != null) { if(!playerSetter) { GameObject player = GameObject.Find("Player"); player.transform.position = this.transform.position; player.transform.rotation = this.transform.rotation; print (transform.parent); if(!parenting) { if(transform.parent != null) { parenting = true; print ("Player Parenting"); player.transform.parent = transform; } } playerSetter =true; } } } }

Viewing all articles
Browse latest Browse all 416

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>