Object resetting after loading a scene for the second time in Unity

I have a game with three scenes, two level scenes and a menu scene. When I press play on my level scene the first time, everything works perfectly, but when I go to the menu scene and then later return to the level scene, certain references on certain scripts were reset. On the scripts shown in the picture below, for example, all but the references that were linked to prefabs in my assets were reset. Why are these references getting reset?

Object resetting after loading a scene for the second time in Unity

Initialize Game Script:

using UnityEngine; using System.Collections; using UnityEngine.Networking; using System.Collections.Generic;  public class InitializeGame : NetworkManager {  NetworkMaster networkMaster;  ConnectionTesterStatus connectionTestResult = ConnectionTesterStatus.Undetermined; string testStatus = "Testing network connection capabilities."; string testMessage = "Test in progress"; string shouldEnableNatMessage  = ""; bool doneTesting = false; bool probingPublicIP = false; bool useNat; int serverPort = 9999; float timer;  public List<NetworkConnection> connectedPlayers = new List<NetworkConnection>();  void OnEnable () {     SetInitialReferences ();     networkMaster.EventStartGame += HostPlayer;     networkMaster.EventJoinGame += ClientPlayer;  }  void OnDestroy(){     if (networkMaster != null) {         networkMaster.EventStartGame -= HostPlayer;         networkMaster.EventJoinGame -= ClientPlayer;     } }  void SetInitialReferences(){     networkMaster = GetComponent<NetworkMaster> (); }  void HostPlayer(){      StartHost ();     InitializePlayer (); }  void ClientPlayer(){     StartClient ();     InitializePlayer (); }  void InitializePlayer(){     if (playerPrefab != null) {          foreach (GameObject obj in GameObject.FindGameObjectsWithTag("Player")) {              if (obj.transform.FindChild ("FirstPersonCharacter").GetComponent<Camera> ().enabled == true) {                 obj.GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().enabled = true;                 print (GameObject.Find ("GameManager").GetComponent<GameManager_ToggleMenu> ().Menu.activeSelf);                 if (GameObject.Find ("GameManager").GetComponent<GameManager_ToggleMenu> ().Menu.activeSelf) {                     GameObject.Find ("GameManager").GetComponent<GameManager_Master> ().CallEventMenuToggle ();                 }             }         }     }  }  } 

Initialize Managers Script:

using UnityEngine; using System.Collections; using UnityEngine.Networking; using UnityEngine.UI; using System.Collections.Generic;  public class InitializeManagers : MonoBehaviour {   public List<GameObject> spawnedObjects = new List<GameObject>(); NetworkMaster networkMaster;  void Awake(){     SetInitialReferences ();     networkMaster.EventHostDisconnected += SetInactiveObjects; }  void OnDisable(){     networkMaster.EventHostDisconnected -= SetInactiveObjects; } void SetInitialReferences(){     networkMaster = GetComponent<NetworkMaster> (); }  public void SetActiveObjects(){     foreach (GameObject obj in spawnedObjects) {         obj.SetActive (true);     } } void SetInactiveObjects(){     foreach (GameObject obj in spawnedObjects) {         if(obj != null)         obj.SetActive (false);     } }  } 

Network Master Script:

using UnityEngine; using System.Collections; using UnityEngine.UI; using System.Collections.Generic; using UnityEngine.Networking; using UnityEngine.SceneManagement;  public class NetworkMaster : MonoBehaviour {  GameManager_Master gameMaster;  public Canvas gameStarterCanvas; public GameObject buttonSALAP; public GameObject buttonSAD;  public delegate void GeneralEventHandler(); public event GeneralEventHandler EventStartGame; public event GeneralEventHandler EventJoinGame; public event GeneralEventHandler EventPlayerDied; public GeneralEventHandler EventRespawnPlayer; public GeneralEventHandler EventHostDisconnected;   private static NetworkMaster _instance ;  void Awake() {     //if we don't have an [_instance] set yet     if(!_instance)         _instance = this ;     //otherwise, if we do, kill this thing     else         Destroy(this.gameObject) ;  }   public void CallEventStartGame(){     if (EventStartGame != null) {         print ("Called");         EventStartGame ();           gameStarterCanvas.gameObject.SetActive (false);     } }  public void CallEventJoinGame(){     if (EventJoinGame != null) {         EventJoinGame ();         gameStarterCanvas.gameObject.SetActive (false);     } }  public void CallEventPlayerDied(){     if (EventPlayerDied != null) {         EventPlayerDied ();     }  }  public void CallEventRespawnPlayer(){     if (EventRespawnPlayer != null) {         EventRespawnPlayer ();     }  }  public void CallEventHostDisconected(){     if (EventHostDisconnected != null) {         EventHostDisconnected ();         gameStarterCanvas.gameObject.SetActive (true);         print ("Hello");     }  }  void SetInitialReferences(){     buttonSAD = gameStarterCanvas.transform.FindChild ("Panel").FindChild ("Scroll View").FindChild("Viewport").FindChild("Content").FindChild("ButtonSAD").gameObject;     buttonSALAP = gameStarterCanvas.transform.FindChild ("Panel").FindChild ("Scroll View").FindChild("Viewport").FindChild("Content").FindChild("ButtonSALAP").gameObject;  }   void OnEnable(){     SetInitialReferences ();      buttonSAD.GetComponent<Button>().onClick.AddListener(() => { CallEventStartGame();});      buttonSALAP.GetComponent<Button>().onClick.AddListener(() => { CallEventStartGame();});      print ("Listeners Added"); }   } 

Replay

Category: c# Time: 2016-07-29 Views: 1

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.194 (s). 12 q(s)