- Home /
Camera follow script not following player?
Okay, I have a player prefab ready to go, and I have another script attached to the camera that instantiates that prefab in the Start function at the beginning of the game. Then, once the camera follow script finds the player object, it starts following it. This script has worked perfectly for a couple days, and I can't figure out why it stopped. But now, it can't find the player at the start. I thought I had solved this with a while loop in the start function, but it doesn't work anymore. The camera doesn't seem to know that the player object exists, because the player is instantiated after the start of the scene, and the camera is in the scene from the beginning. So this is the modified camera follow script I have.
using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour
{
public float xMargin = 1f; // Distance in the x axis the player can move before the camera follows.
public float yMargin = 1f; // Distance in the y axis the player can move before the camera follows.
public float xSmooth = 8f; // How smoothly the camera catches up with it's target movement in the x axis.
public float ySmooth = 8f; // How smoothly the camera catches up with it's target movement in the y axis.
public Vector2 maxXAndY; // The maximum x and y coordinates the camera can have.
public Vector2 minXAndY; // The minimum x and y coordinates the camera can have.
private Transform player; // Reference to the player's transform.
private bool playerExists = false;
void Start ()
{
// Setting up the reference.
while(!playerExists){
player = GameObject.FindGameObjectWithTag("Player").transform;
if(player != null){
playerExists = true;
}
}
}
bool CheckXMargin()
{
// Returns true if the distance between the camera and the player in the x axis is greater than the x margin.
return Mathf.Abs(transform.position.x - player.position.x) > xMargin;
}
bool CheckYMargin()
{
// Returns true if the distance between the camera and the player in the y axis is greater than the y margin.
return Mathf.Abs(transform.position.y - player.position.y) > yMargin;
}
void FixedUpdate ()
{
TrackPlayer();
}
void TrackPlayer ()
{
// By default the target x and y coordinates of the camera are it's current x and y coordinates.
float targetX = transform.position.x;
float targetY = transform.position.y;
// If the player has moved beyond the x margin...
if(CheckXMargin())
// ... the target x coordinate should be a Lerp between the camera's current x position and the player's current x position.
targetX = Mathf.Lerp(transform.position.x, player.position.x, xSmooth * Time.deltaTime);
// If the player has moved beyond the y margin...
if(CheckYMargin())
// ... the target y coordinate should be a Lerp between the camera's current y position and the player's current y position.
targetY = Mathf.Lerp(transform.position.y, player.position.y, ySmooth * Time.deltaTime);
// The target x and y coordinates should not be larger than the maximum or smaller than the minimum.
targetX = Mathf.Clamp(targetX, minXAndY.x, maxXAndY.x);
targetY = Mathf.Clamp(targetY, minXAndY.y, maxXAndY.y);
// Set the camera's position to the target position with the same z component.
transform.position = new Vector3(targetX, targetY, transform.position.z);
}
}
At the start of the scene, I get this one error:
NullReferenceException: Object reference not set to an instance of an object CameraFollow.Start () (at Assets/Scripts/CameraFollow.cs:21)
Which is pointing to this line:
player = GameObject.FindGameObjectWithTag("Player").transform;
So obviously the camera can't find the player, but then I think it stops looking, and I don't know why. Because I get other errors during the game that tell me the player object is null. Does anybody know why this isn't working or how to fix this error? Thanks in advance for any help.
Edit:
I just found out that it works if I put the player prefab into the scene before loading it, but I don't want that, I want the player to be instantiated after the scene starts. That's when the script doesn't work.
Answer by DoctorSauce · Dec 28, 2013 at 05:22 PM
I fixed it. I just got rid of the start function and made another function for finding the player, then just performed that function in the Update function if the player hadn't been found yet.