- Home /
Null Reference when trying to instantiate a prefab not in the scene
So I'm driving myself insane trying to nail down this particular bug. Basically, I have a multiplayer only game, and when you first start it up, you are presented with a toolbar of buttons to select the color for your tank. Upon pressing the button, it SHOULD instantiate the tank, the child GameObject turret, and it's associated scripts. The problem is, I can't seem to pass the public variable along to the proper scripts. I know this probably doesn't make much sense, so here's some code. First the NetworkManager script :
public class NetworkManager : MonoBehaviour {
public GameObject selectedTank;
private int selectedToolbar = 0;
private string[] toolbarStrings = { "Gray", "Green", "Purple", "Red", "Yellow" };
public bool canSelectColor = true;
void Start ()
{
PhotonNetwork.ConnectUsingSettings("alpha");
}
void OnGUI()
{
if (canSelectColor)
{
selectedToolbar = GUI.Toolbar(new Rect(50, 10, Screen.width - 100, 30), selectedToolbar, toolbarStrings);
if (GUI.changed)
{
if (0 == selectedToolbar)
{
selectedTank = (GameObject)Resources.Load("grayTank");
canSelectColor = false;
PhotonNetwork.JoinRandomRoom();
}
if (1 == selectedToolbar)
{
selectedTank = (GameObject)Resources.Load("greenTank");
canSelectColor = false;
}
if (2 == selectedToolbar)
{
selectedTank = (GameObject)Resources.Load("purpleTank");
canSelectColor = false;
}
if (3 == selectedToolbar)
{
selectedTank = (GameObject)Resources.Load("redTank");
canSelectColor = false;
}
if (4 == selectedToolbar)
{
selectedTank = (GameObject)Resources.Load("yellowTank");
canSelectColor = false;
}
}
GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
}
}
//void OnJoinedLobby()
//{
// PhotonNetwork.JoinRandomRoom();
//}
void OnPhotonRandomJoinFailed()
{
PhotonNetwork.CreateRoom(null);
}
void OnJoinedRoom()
{
GameObject tank = PhotonNetwork.Instantiate(selectedTank.name, Vector3.zero, Quaternion.identity, 0);
FireControl fireControl = tank.GetComponentInChildren<FireControl>();
fireControl.enabled = true;
TurretControl turretControl = tank.GetComponentInChildren<TurretControl>();
turretControl.enabled = true;
TankControl tankControl = tank.GetComponent<TankControl>();
tankControl.enabled = true;
}
}
And the TurretControl script, where all the problems seem to be happening :
public class TurretControl : MonoBehaviour {
private GameObject tank;
public GameObject selectedTank;
// Use this for initialization
void Start ()
{
//tank = GameObject.Find("redTank(Clone)");
selectedTank = GetComponent<NetworkManager>().selectedTank;
}
void Awake()
{
}
// Update is called once per frame
void Update ()
{
Debug.Log(selectedTank.name);
Vector3 turretPos = new Vector3(selectedTank.transform.position.x, selectedTank.transform.position.y, -3);
Vector3 rotation = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
float angle = Mathf.Atan2(rotation.x, rotation.y * -1) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle - 90));
transform.position = turretPos;
}
}
What is happening is it spawns in the grayTank just fine, but the turret is drawn in the wrong location, not locked to the tank properly. Also Unity is throwing NullReference exceptions left and right, saying that selectedTank is null. I don't understand how that can be when it is clearly spawning the tank onto the screen. Any help is greatly appreciated.
Your answer
Follow this Question
Related Questions
Referencing gameObject from script after Instantiate 0 Answers
Photon.Instantiate causes second-created player to swap places with first 1 Answer
Photon Instantiate object not disappear after dc master client 0 Answers
NullReference Exception when adding a class 2 Answers
[Closed]NullReferenceException on Object Instantiation onto game world 2 Answers