- Home /
Multiplayer Player Prefab: NullReferenceException: Object reference not set to an instance of an object
Hi!
I have a problem! I want to test multiplayer a little bit so i set up a scene with network manager and the network identity on the player prefab! i dragged the player prefab in the slot in the network manager! the camera control is seperatly and not connected with the player prefab! offline everything works ok, but when i start the multiplayer test i get the following NullReferenceException:
NullReferenceException: Object reference not set to an instance of an object CameraOrbit.Start () (at Assets/Scripts/Camera Scripts/CameraOrbit.cs:29)
NullReferenceException: Object reference not set to an instance of an object CameraOrbit.HandleCamera () (at Assets/Scripts/Camera Scripts/CameraOrbit.cs:64) CameraOrbit.Update () (at Assets/Scripts/Camera Scripts/CameraOrbit.cs:45)
Player Movement is working correctly but the camera wont. ive tried several things now but i cant find the right solution. the error occurs in the camera script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraOrbit : Orbit {
public Vector3 targetOffset = new Vector3 (0, 2, 0);
public Vector3 cameraPositionZoom = new Vector3 (-0.5f, 0, 0);
public float cameraLenght = -10f;
public float cameraLenghtZoom = -5f;
public Vector2 orbitSpeed = new Vector2 (0.01f, 0.01f);
public Vector2 orbitOffset = new Vector2 (0, -0.8f);
public Vector2 angleOffset = new Vector2 (0, -0.25f);
private float _zoomValue;
private Vector3 _cameraPositionTemp;
private Vector3 _cameraPosition;
private Transform _playerTarget;
private Camera _mainCamera;
void Awake ()
{
}
// Use this for initialization
void Start () {
_playerTarget = GameObject.FindGameObjectWithTag ("Player").transform;
sphericalVectorData.Lenght = cameraLenght;
sphericalVectorData.Azimuth = angleOffset.x;
sphericalVectorData.Zenith = angleOffset.y;
_mainCamera = Camera.main;
_cameraPositionTemp = _mainCamera.transform.localPosition;
_cameraPosition = _cameraPositionTemp;
}
// Update is called once per frame
void Update () {
HandleCamera ();
}
void HandleCamera ()
{
if ( Input.GetMouseButton (1) )
{
sphericalVectorData.Azimuth += Input.GetAxis ("Mouse X") * orbitSpeed.x;
sphericalVectorData.Zenith += Input.GetAxis ("Mouse Y") * orbitSpeed.y;
sphericalVectorData.Zenith = Mathf.Clamp (sphericalVectorData.Zenith + orbitOffset.x, orbitOffset.y, 0f);
}
float distanceToObject = _zoomValue;
float deltaDistance = Mathf.Clamp (_zoomValue, distanceToObject, -distanceToObject);
sphericalVectorData.Lenght += (deltaDistance - sphericalVectorData.Lenght);
Vector3 lookAt = targetOffset;
lookAt += _playerTarget.position;
base.Update ();
transform.position += lookAt;
transform.LookAt (lookAt);
if ( _zoomValue == cameraLenghtZoom )
{
Quaternion targetRotation = transform.rotation;
targetRotation.x = 0f;
targetRotation.z = 0f;
_playerTarget.rotation = targetRotation;
}
_cameraPosition = _cameraPositionTemp;
_zoomValue = cameraLenght;
}
}
ive tried to set the _playerTarget variable public and i saw that this variable stays empty after start. i think thats the problem. can anyone help? Thank you
Your answer
Follow this Question
Related Questions
Null reference exception on Screenpointtoray (Multiplayer) 1 Answer
How to have player camera Render enemy but not itself with same tag and camera clipping mask? 0 Answers
How do I make a Game Object invisible? 1 Answer
How do you use a camera from another scene on your skybox? 1 Answer
Camera Board Game - Multiplayer,Multiplayer camera - best practice 1 Answer