- Home /
Null Reference Exception when using if(!islocalplayer) destroy
So Im a complete newbie in networking, im using a network manager. I added a destroy if not local player.
However for some reason I get a null reference exception. The network identity is set to local player authority and I added the player prefab, ticked spawn automatically in the network manager. Here's my full code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;
using UnityEngine;
public class player_controller : NetworkBehaviour {
//private int DebugComment;
private int frame;
public bool onVehicle = false;
public bool nearPlanet = false;
public float speed;
private Vector3 moveDirection;
public float mouseSensitivity;
public float clampAngle;
//public Camera cameraOBJ;
//public Camera minimapcameraOBJ;
Animator droidAnim;
Rigidbody PlayerRB;
Transform mainCam;
Transform miniCam;
public Vector3 offset;
private float rotationY = 0.0f; // rotation around the up/y axis
private float rotationX = 0.0f; // rotation around the right/x axis
void Start()
{
/*if (!isLocalPlayer)
{
destroy(this);
return;
}*/
mainCam = GameObject.Find("FirstPersonCamera").GetComponent<Transform>();
miniCam = GameObject.Find("MinimapCamera").GetComponent<Transform>();
offset = new Vector3(0f, 1000f, 0f);
PlayerRB = GetComponent<Rigidbody>();
Vector3 rotation = transform.localRotation.eulerAngles;
rotationY = rotation.y;
rotationX = rotation.x;
//Camera cameracopy = Instantiate<Camera>(cameraOBJ);
//Camera minimapcameracopy = Instantiate<Camera>(minimapcameraOBJ);
}
void Update()
{
Debug.Log("isLocalPlayer == " + isLocalPlayer);
//camera
Vector3 camPos = this.transform.position;
mainCam.transform.position = camPos;
Vector3 miniCamPos = this.transform.position + offset;
miniCam.transform.position = miniCamPos;
//DebugComment++;
//Outer space
if (onVehicle == false && nearPlanet == false)
{
//rotate
float mouseX = Input.GetAxis("Mouse X");
float mouseY = -Input.GetAxis("Mouse Y");
rotationY += mouseX * mouseSensitivity * Time.deltaTime;
rotationX += mouseY * mouseSensitivity * Time.deltaTime;
rotationX = Mathf.Clamp(rotationX, -clampAngle, clampAngle);
if(rotationX < -30)
{
rotationX = -30;
}
Quaternion localRotation = Quaternion.Euler(rotationX, rotationY, 0.0f);
transform.rotation = localRotation;
//end of rotate
//move
moveDirection = (transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal"));
moveDirection = moveDirection.normalized * speed;
if (Input.GetButton("Jump"))
{
moveDirection.y = speed;
}
if (Input.GetKey(KeyCode.LeftShift))
{
moveDirection.y = -speed;
}
PlayerRB.velocity = moveDirection;
if(Input.GetButton("Vertical") || Input.GetButton("Horizontal") || Input.GetKey(KeyCode.LeftShift) || Input.GetButton("Jump"))
{
PlayerRB.isKinematic = false;
if (frame > 5)
{
PlayerRB.isKinematic = true;
frame = 0;
}else
{
PlayerRB.isKinematic = false;
frame++;
}
}else
{
PlayerRB.isKinematic = true;
}
//end of move
}
//Near Planet
if (onVehicle == false && nearPlanet == true)
{
//rotate
float mouseX = Input.GetAxis("Mouse X");
float mouseY = -Input.GetAxis("Mouse Y");
rotationY += mouseX * mouseSensitivity * Time.deltaTime;
rotationX += mouseY * mouseSensitivity * Time.deltaTime;
rotationX = Mathf.Clamp(rotationX, -clampAngle, clampAngle);
Quaternion localRotation = Quaternion.Euler(0.0f, rotationY, 0.0f);
transform.rotation = localRotation;
//end of rotate
}
}
}
This is the error I got NullReferenceException: Object reference not set to an instance of an object player_controller.Update () (at Assets/WarpSpace/Obj/ObjFile/Droid/Scripts/player_controller.cs:52)
Thanks
Answer by Captain_Pineapple · Apr 14, 2018 at 09:24 AM
Depending on what you want to do:
If you just want remove the playerController Script on Objects that are not the currents Player property don't use destroy(this) but rather:
getComponent<player_controller>().enabled = false;
If you really want to remove the GameObject as a whole then use Destroy(gameObject);
Thanks Alot! Although I did that, I still get the same error message. I removed that script and my islocalplayer just keeps alternating true and false after 3 frames of false when starting up. There are no scripts attatched to the 2 cameras I have.
I'm not sure if i can help you with that problem since i'd probably need to see everything you do, especially since you use networking.
However a workaround for your case might be to just encase all unwanted code in the update function in an if-Statement:
if(isLocalPlayer)
{
// dostuff
}