I have the following code. Line 55 gives me a null reference exception and I can’t figure out why…
using UnityEngine;
using System.Collections;
public class NetworkManager : MonoBehaviour {
public GameObject Player;
public GameObject rainSystem;
public GameObject snowSystem;
public GameObject butterflies;
public GameObject snowDust;
public GameObject mistCloud;
public GameObject windyLeaves;
private Vector3 playerPos;
private Transform playerTransform;
private const string typeName = "TestWeatherGame";
private const string gameName = "Click me to join!!";
private HostData[] hostList;
public Transform[] spawnPoints;
private GameObject playerInstantiation;
public bool playerHasSpawned = false;
void Awake(){
playerHasSpawned=false;
}
void Start ()
{
playerHasSpawned=false;
}
void Update()
{
if (playerHasSpawned)
{
moveChildren();
}
}
private void moveChildren()
{
Debug.Log ("inside move children");
Debug.Log (playerInstantiation.transform.position); //line that gives me error
}
private void RefreshHostList()
{
MasterServer.RequestHostList(typeName);
}
void OnMasterServerEvent(MasterServerEvent msEvent)
{
if (msEvent == MasterServerEvent.HostListReceived)
hostList = MasterServer.PollHostList();
}
private void JoinServer(HostData hostData)
{
Network.Connect (hostData);
}
void OnConnectedToServer()
{
SpawnPlayer();
}
private void StartServer()
{
Network.InitializeServer (4, 25000, !Network.HavePublicAddress());
MasterServer.RegisterHost(typeName, gameName);
}
void OnServerInitialized()
{
SpawnPlayer();
}
private void SpawnPlayer()
{
Transform point = spawnPoints[Random.Range (0, spawnPoints.Length)];
Vector3 pos = point.position;
GameObject playerInstantiation = Network.Instantiate (Player, new Vector3(pos.x, pos.y, pos.z), Quaternion.identity, 0) as GameObject;
rainSystem.transform.parent=playerInstantiation.transform;
snowDust.transform.parent=playerInstantiation.transform;
snowSystem.transform.parent=playerInstantiation.transform;
butterflies.transform.parent=playerInstantiation.transform;
windyLeaves.transform.parent=playerInstantiation.transform;
Debug.Log (playerInstantiation.transform.position); //yet this exact same line is fine...
playerHasSpawned=true;
Debug.Log (playerHasSpawned);
}
void OnGUI()
{
if (!Network.isClient && !Network.isServer)
{
if (GUI.Button (new Rect(100,100,250,100), "Start Server"))
StartServer();
if (GUI.Button (new Rect(100,250,250,100), "Refresh Game List"))
RefreshHostList();
if (hostList != null)
{
for (int i = 0; i < hostList.Length; i++)
{
if (GUI.Button(new Rect(400,100 +(110 *i), 300, 100), hostList*.gameName))*
_ JoinServer(hostList*);*_
* }*
* }*
}
I can read playerInstantiate.transform.position on line 108 with my debug.log but not at line 55?!?! if I comment out line 55 my game runs fine but I need to grab playerInstantiations transform.position there so I can move objects to it.
Any help would be appreciated.