Hi,
I’m attaching this script to my camera so that it followes the player smoothly, problem is it keeps giving me a NullReferenceException
var player : Transform;
var smoothValue = 0.8;
var startYPosition : float;
function Start()
{
startYPosition = transform.position.y;
transform.position.x = player.position.x;
transform.position.z = player.position.z;
}
function Update ()
{
transform.postion = Vector3(Mathf.Lerp(transform.postion.x, player.postion.x, Time.deltaTime * smoothValue), startYPosition, Mathf.Lerp(transform.postion.z, player.postion.z, Time.deltaTime * smoothValue));
}
I’m assigning the “player” variable in the inspector, what is it that I’m not referencing?
Any help would be greatly appreciated
thanks,
-Chris
If you double click on the NullReferenceException it will take you to the line that is causing the error. If you told us which line it points to, it would be easier to fix.
Line 12, which is this
transform.postion = Vector3(Mathf.Lerp(transform.postion.x, player.postion.x, Time.deltaTime * smoothValue), startYPosition, Mathf.Lerp(transform.postion.z, player.postion.z, Time.deltaTime * smoothValue));
one or more of your variables are null or nothing. my guess that its player, try changing players type from Transform to GameObject, that may work this error is kinda buggy like that. make the script check through its variables to see if any are null, if the are make it say which one are null.
Thanks for the reply, I have been going over the script, trying diffrent things, can’t seem to make it work. I tried reimporting assigning the script, restarting unity many times, and avariety of similiar things. I’ve run into this problem before with the Null Reference, but it has never been this persistant, are you sure it’s not my script?
Thanks!
You’ve spelled ‘position’ as ‘postion’ a great number of times. To check if the inspector assignment is correct just do
Debug.Log(player.gameObject.name);
In the Start() method. If you get nothing or a null pointer exception then the assignment is incorrect.
Thanks for the help, that was the issue, I can’t believe I missed that!