- Home /
Player Singleton and Spawning in new scene issue
So I have a strange issue when it comes to spawning my player prefab with a singleton attached to it which allows the player data to persist between scenes. The issue is when I activate the trigger by pressing "e" when the raycast hits the button to enter the new scene the player won't spawn at the spawn point, but instead spawns somewhere else in the scene and falls.I partly fixed the spawning issue by moving the whole level within the scene to where the player was falling, but I would like to use a better fix than that. I am not quite sure if this is due to a singleton being attached to the player or if I have to put a spawn controller on the player as well. The spawn point does work though when the PlayerControl1 script is not attached, but without the singleton the gui textures and texts disappear completely and all my player is left with is the weapons and controls. Help would be greatly appreciated because I've tried to find this issue I'm having on the web and I can't find it at all.
Player Control 1 script is the singleton script attached to my player prefab
 using UnityEngine;
 using System.Collections;
 using System;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 public class PlayerControl1 : MonoBehaviour 
 {
     public static PlayerControl1 control;
 
     public int P1Health;
     public int P1Armor;
 
     public bool meleeWeapon;
     public bool sideArm;
     public bool primaryWeapon;
     public bool secondaryWeapon;
     public bool testdol;
 
     //Initializes player data
     void Awake () 
     {
         P1Health = 100;
         P1Armor = 0;
 
         meleeWeapon = true;
         sideArm = false;
         primaryWeapon = false;
         secondaryWeapon = false;
 
         testdol = true;
         
         //Makes sure player data doesn't get destroyed when loading a scene
         if(control == null)
         {
             DontDestroyOnLoad(gameObject);
             control = this;
         }
 
         //Makes sure only one player control is present in each scene
         else if(control != this)
         {
             Destroy (gameObject);
         }
     }
 
     void Update()
     {
         //Keeps melee weapon out when progressing through scenes
         if(Input.GetKeyDown ("1"))
         {
             meleeWeapon = true;
             sideArm = false;
             primaryWeapon = false;
             secondaryWeapon = false;
         }
 
         //Keeps sidearm weapon out when progressing through scenes
         if(Input.GetKeyDown ("2"))
         {
             meleeWeapon = false;
             sideArm = true;
             primaryWeapon = false;
             secondaryWeapon = false;
         }
 
         //Keeps primary weapon out when progressing through scenes
         if(Input.GetKeyDown ("3"))
         {
             meleeWeapon = false;
             sideArm = false;
             primaryWeapon = true;
             secondaryWeapon = false;
         }
 
         //Keeps secondary weapon out when progressing through scenes
         if(Input.GetKeyDown ("4"))
         {
             meleeWeapon = false;
             sideArm = false;
             primaryWeapon = false;
             secondaryWeapon = true;
         }
     }
     
     // Saves Player Data
     public void Save () 
     {
         BinaryFormatter bf = new BinaryFormatter();
 
         //Creates the save file for the player data
         FileStream file = File.Create (Application.persistentDataPath + "/PlayerData.dat");
 
         PlayerData data = new PlayerData ();
         data.P1Health = P1Health;
         data.P1Armor = P1Armor;
         data.meleeWeapon = meleeWeapon;
         data.sideArm = sideArm;
         data.primaryWeapon = primaryWeapon;
         data.secondaryWeapon = secondaryWeapon;
 
         bf.Serialize (file, data);
         file.Close();
     }
 
     // Loads Player Data
     public void Load()
     {
         //Checks to see if the file PlayerData.dat exists and if it does it loads the file
         if(File.Exists(Application.persistentDataPath + "/PlayerData.dat"))
         {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open (Application.persistentDataPath + "/PlayerData.dat", FileMode.Open);
             PlayerData data = (PlayerData)bf.Deserialize(file);
             file.Close();
 
             P1Health = data.P1Health;
             P1Armor = data.P1Armor;
             meleeWeapon = data.meleeWeapon;
             sideArm = data.sideArm;
             primaryWeapon = data.primaryWeapon;
             secondaryWeapon = data.secondaryWeapon;
         }
     }
 }
 
 //Makes player data serializable for saving and loading
 [Serializable]
 class PlayerData
 {
     public int P1Health;
     public int P1Armor;
     public bool meleeWeapon;
     public bool sideArm;
     public bool primaryWeapon;
     public bool secondaryWeapon; 
 }
LevelController script attached to my player prefab
 using UnityEngine;
 using System.Collections;
 
 public class LevelController : MonoBehaviour 
 {
     public GUIText medbayText;
     public string medText;
     public float range = 2.0f;
     public bool mEnter;
     Ray ray;
     RaycastHit hit;
 
     void Awake()
     {
         ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     }
 
     // Update is called once per frame
     void Update () 
     {
         Debug.DrawRay (this.transform.position, this.transform.forward * range, Color.red);
         if(Physics.Raycast (this.transform.position, this.transform.forward, out hit, range))
         {
             Debug.Log(hit.collider.gameObject.name);
             if(hit.collider.gameObject.tag == "MedbayLevel")
             {
                 medText = "Press E To Enter Medbay";
                 medbayText.text = medText.ToString ();
                 mEnter = true;
                 if(Input.GetKey ("e"))
                 {
                     Application.LoadLevel("MedicalBay");
                 }
             }
             else
             {
                 mEnter = false;
                 medText = "";
                 medbayText.text = medText.ToString ();
             }
         }
     }
 }
MedbaySpawnPoint script attached to the empty GameObject within the new scene
 using UnityEngine;
 using System.Collections;
 
 public class MedbaySpawnPoint : MonoBehaviour 
 {
     public GameObject MedBayPlayer;
     // Use this for initialization
     void OnLevelWasLoaded () 
     {
         GameObject spawnerMed = (GameObject)Instantiate(MedBayPlayer, transform.position, transform.rotation);
     }
 }
 
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                