Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by bromley · May 27, 2016 at 07:56 AM · instantiatescenespawnload

Load Player Position Problem

i created a script that allow you to save player position and rotation, using the Instantiate instance to create a temporary spawn, so if you load the game, player have to start on the temporary spawn.

Anyway the scripts are these:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System;
 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;
 using UnityEngine.SceneManagement;
 
 public class SaveSystem : MonoBehaviour {
 
     GameObject player;
     GameObject cam;
     GameObject start;
 
 
     Start_Scene startScript;
 
     private Scene scene;
 
 
     void Start()
     {
         DontDestroyOnLoad (this);
         if (FindObjectsOfType (GetType ()).Length > 1) 
         {
             Destroy (gameObject);
         }
 
         player = GameObject.Find ("PlayerController");
         cam = GameObject.Find ("Camera");
         start = GameObject.Find ("Start Scene");
 
 
         startScript = start.GetComponent<Start_Scene> ();
 
 
     }
 
     public void SaveData () 
     {
         BinaryFormatter bf = new BinaryFormatter ();
         FileStream file = File.Create (Application.persistentDataPath + "/Data.dat");
         PlayerData data = new PlayerData ();
 
 
         scene = SceneManager.GetActiveScene ();
         data.sceneName = scene.name;
 
         data.posx = player.transform.position.x;
         data.posy = player.transform.position.y;
         data.posz = player.transform.position.z;
 
         data.rotx = cam.transform.localRotation.x;
         data.roty = cam.transform.localRotation.y;
         data.rotz = cam.transform.localRotation.z;
         data.rotw = cam.transform.localRotation.w;
 
         bf.Serialize (file, data);
         file.Close ();
     }
 
     public void LoadData()
     {
         if(File.Exists (Application.persistentDataPath + "/Data.dat"))
         {
             BinaryFormatter bf = new BinaryFormatter ();
             FileStream file = File.Open (Application.persistentDataPath + "/Data.dat", FileMode.Open);
             PlayerData data = (PlayerData)bf.Deserialize (file);
 
             file.Close ();
 
             PlayerPrefs.DeleteAll ();
 
             SceneManager.LoadScene (data.sceneName, LoadSceneMode.Single);
 
             Vector3 newPlayerPos = new Vector3 (data.posx, data.posy, data.posz);
             Quaternion newPlayerRot = new Quaternion (0, data.roty, 0, data.rotw);
 
             startScript.LoadDataState (newPlayerPos, newPlayerRot);
 
         }
     }
 }
 
 [Serializable]
 class PlayerData
 {
     public string sceneName;
 
     public float posx;
     public float posy;
     public float posz;
 
     public float rotx;
     public float roty;
     public float rotz;
     public float rotw;
 }


using UnityEngine; using System.Collections;

public class Start_Scene : MonoBehaviour {

 GameObject cam;
 GameObject lockCam;
 GameObject player;
 GameObject spawn;
 GameObject sceneMan;

 public GameObject dynamicSpawn;

 public string spawnState = "Default";

 private CameraMouseLook camScript;
 private Lock lockScript;
 private PlayerMovement playScript;
 private Scene_Manager sceneScript;

 private IEnumerator camRotationRoutine;
 private IEnumerator camRotationRoutine2;

 private float delay = 0.25F;

 private Vector3 playPos;
 private Quaternion playRot;

 void Start () 
 {
     DontDestroyOnLoad (this);
     if (FindObjectsOfType (GetType ()).Length > 1) 
     {
         Destroy (gameObject);
     }

     spawnState = PlayerPrefs.GetString ("Spawn State", "Default");

     player = GameObject.Find ("PlayerController");
     cam = GameObject.Find ("Camera");
     lockCam = GameObject.Find ("Lock");
     spawn = GameObject.Find ("Spawn_" + spawnState);
     sceneMan = GameObject.Find ("Scene_Manager");

     playScript = player.GetComponent<PlayerMovement> ();
     camScript = cam.GetComponent<CameraMouseLook> ();
     lockScript = lockCam.GetComponent<Lock> ();
     sceneScript = sceneMan.GetComponent<Scene_Manager> ();

     if (spawnState != "Default" && spawn != null) 
     {
         camRotationRoutine = camRotation (delay);
         StartCoroutine (camRotationRoutine);

         spawnState = "From" + sceneScript.sceneName;
     }

     GameObject fader = GameObject.Find ("Screen Fader");
     Fade_Screen_Scene scriptFader = fader.GetComponent<Fade_Screen_Scene> ();
     scriptFader.CallStartScene ();

     SaveState ();
 }

 public void SaveState ()
 {
     PlayerPrefs.SetString ("Spawn State", spawnState);
 }

 public void LoadDataState(Vector3 pos, Quaternion rot)
 {
     playPos = pos;
     playRot = rot;

     camRotationRoutine2 = camRotation2 (delay);
     StartCoroutine (camRotationRoutine2);
 }

 IEnumerator camRotation (float delay)
 {
     camScript.blockCamera = true;
     playScript.playerBlocked = true;

     yield return new WaitForSeconds (delay);

     player.transform.position = spawn.transform.position;
     cam.transform.localRotation = spawn.transform.localRotation;
     camScript.DefaultRotation ();

     yield return new WaitForSeconds (delay);

     camScript.blockCamera = false;
     playScript.playerBlocked = false;
     StopCoroutine (camRotationRoutine);
 }

 IEnumerator camRotation2 (float delay)
 {
     camScript.blockCamera = true;
     playScript.playerBlocked = true;

     yield return new WaitForSeconds (delay);

     GameObject newSpawn = (GameObject) Instantiate (dynamicSpawn, playPos, playRot);
     newSpawn.name = dynamicSpawn.name;

     player.transform.position = newSpawn.transform.position;
     cam.transform.localRotation = newSpawn.transform.localRotation;
     camScript.DefaultRotation ();
     lockScript.DefaultPosition ();

     yield return new WaitForSeconds (delay);

     camScript.blockCamera = false;
     playScript.playerBlocked = false;
     StopCoroutine (camRotationRoutine2);
 }

}

The first one is the SaveSystem script that allow you to save and load game, the second one is the script that contains references of the temporary spawn.

The problem is in the second script: when player position and camera rotation have to get values of dynamicSpawn position and rotation (the temporary spawn), an error appears:

MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. Start_Scene+c__Iterator9.MoveNext () (at Assets/Script/Start_Scene.cs:107)

I think that during the loading Scene, player and camera are not already loaded.

I don't know how to fix, please help.

Comment
Add comment · Show 1
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
avatar image bromley · May 28, 2016 at 06:27 AM 0
Share

anyone? i don't solved it yet

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Deathtruth · May 28, 2016 at 01:20 PM

This answer I believe is the issue you are having,

http://answers.unity3d.com/questions/880252/dontdestroyonload-error.html

Comment
Add comment · Show 1 · Share
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
avatar image bromley · May 29, 2016 at 06:08 AM 0
Share

the problem is another one: my SaveSystem object doesn't destroy itself and so, when i re-load the scene, it doesn't find all gameobjects ("player", "camera", ecc...")

avatar image
0

Answer by bromley · May 30, 2016 at 07:04 AM

I solved it... the cause of this problem was that loading of the new scene was too long and the other scripts tried to call objects that were not loaded yet.

So i created an IEnumerator and added this:

 SceneManager.LoadScene (data.sceneName, LoadSceneMode.Single);
 AsyncOperation async = SceneManager.LoadSceneAsync (data.sceneName);
 
     while (!async.isDone) 
     {
         yield return null;
     }


Now with this script, all next functions must be wait until loading scene is finished. And works good!

Comment
Add comment · Share
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

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

51 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to tell which enemys to create in new battle scene 1 Answer

Spawn many object 1 Answer

HELP Game Scene 8 (-1) Cannot be loaded because it has not been added to the build settings 1 Answer

Simple and Quick Fix PickUp script 2 Answers

Input.GetMouseButton on multiple objects 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