Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by SHOOTERNsK · Jun 20, 2014 at 08:11 PM · playerscenespawnsingletonspawnpoint

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);
     }
 }
 





Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

enemy targeting player in different scene 1 Answer

Player Respawn After Death 1 Answer

Problem with spawn player with RPC calls(c #) 1 Answer

Player not spawning in right place. 1 Answer

enemy AI doesnt work when enemy is spawned but does if it is already in the scene 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges