CameraController (object reference not set to an instance of an object)
I followed the roll-a-ball tutorial and camera worked and followed the player, but when I made the game multiplayer and it was spawning players as Player(Clone), I tried to remake the script as following (because it's not working if I put a prefab onto the main camera):
using UnityEngine; using System.Collections;
public class CameraController : MonoBehaviour {
private GameObject rb;
private Vector3 offset;
void Start ()
{
GameObject player = GameObject.FindWithTag ("Player");
player = rb;
rb.GetComponent<GameObject> ();
offset = transform.position - rb.transform.position;
}
void LateUpdate ()
{
transform.position = rb.transform.position + offset;
}
}
And it was giving me this error (object reference not set to an instance of an object) ... I don't know what I did wrong... I'm appreciating any response ....
Answer by Zoogyburger · Mar 10, 2016 at 10:51 PM
Why are you making the RigidBody equal to the player? Try something more like this:
private Transform Player;
private Vector3 offset;
void Start ()
{
Player = GameObject.FindWithTag ("Player").transform;
Player = rb;
rb.GetComponent<GameObject> ();
offset = transform.position - rb.transform.position;
}
void LateUpdate ()
{
transform.position = rb.transform.position + offset;
}
Also here is a real simple camera follow player script:
public Transform target;
void Update()
{
if (target)
{
transform.position = Vector3.Lerp (transform.position, target.position, 0.1f) + new Vector3 (0, 0, -10);
}
}
}
It's not working... If I use the first method it's giving me the same error and if I use the second method it's not seeing the player and it's not following it ... That's maybe because I need to put a prefab to the camera and that's why it's not working like this ..... But thanks anyway !
@andreighinea1 Is it a 2d game or a 3d game? Because this scrip was sorta setup for a 2d game. Try this ins$$anonymous$$d:
public Transform target;
void Update()
{
if (target)
{
transform.position = Vector3.Lerp (transform.position, target.position, 0.1f);
}
}
}
$$anonymous$$ake sure the script is on the $$anonymous$$ain Camera and in its Inspector set the Target to be the Player.
It's a 3D game and I'm using the Network$$anonymous$$anager and when I put the player prefab into the Network$$anonymous$$anager it's spawning it with the camera but it's not following the player And i also searched for a 3d script for cameracontroller and I found that: using UnityEngine; using System.Collections;
public class CameraController : $$anonymous$$onoBehaviour { public GameObject target; public float smoothing = 2f; public Vector3 offset;
void Start()
{
}
void FixedUpdate()
{
if(GameObject.FindGameObjectWithTag("Player") == target)
{
offset = new Vector3(0.0f, target.GetComponent<PlayerController>().size / 2 + 15, -target.GetComponent<PlayerController>().size / 2- 20);
}
transform.position = target.transform.position + offset;
//transform.LookAt(target.transform.position);
}
}
But it says when I make it 'networked' it's not following the player when I assign to it a prefab of the player ... But if I put the player into the scene and I assign it from scene it's working ... But I want to make it multiplayer ....
Ok, don't assign the Target at all. Ins$$anonymous$$d tag your player "Player" and update your Camera script to this:
public Transform target;
void Update()
{
target = GameObject.FindWithTag ("Player").transform;
if (target)
{
transform.position = Vector3.Lerp (transform.position, target.position, 0.1f)+ new Vector3 (0, 0, -10);
}
}
}
Thank you very much ! This script really helped me ! (I had to replace the Update with FixedUpdate) Now it's following the player, yeah it's still giving me the error object reference not .... But for me is good at least that its working, one single thing is that if I go for something like 1000 meters down with speed the player is not visible at all ... But for now is good, because the player can stop for a little to wait for camera ...
Hey @andreighinea1 Glad my script helped you. It's really weird that you still get that error and the Camera lags. Here is my complete script (Try just Update again)
public Transform target;
void Update()
{
target = GameObject.FindWithTag ("Player").transform;
if (target)
{
transform.position = Vector3.Lerp (transform.position, target.position, 1f)+ new Vector3 (0, 0, -10);
}
}
}
Replace the trouble line with this:
Player = GameObject.Instantiate(Player) as Transform;
And assign the Camera target to be your Player Prefab.
Your answer
Follow this Question
Related Questions
Object follows AR Camera 1 Answer
How to Fix this issue 1 Answer
HTC Vive camera deformed when assigned a parent 0 Answers
Objects close to camera jitter? 0 Answers
My camera jitters a lot. Any fix? 0 Answers