I’m trying to set player, that I instantiate, to Camera_behaviour’s playerPrefab variable so camera could follow it. It gives an error “UnassignedReferenceException: The variable playerPrefab of ‘Camera_behaviour’ has not been assigned. You probably need to assign the playerPrefab variable of the Camera_behaviour script in the inspector.”
I can’t assign it in inspector, because I need to reference the instance of playerPrefab. Anyone could help me how to fix this or give way to get same results by different means in scripting?
Thank you.
Camera_behaviour.js
public var playerPrefab : Transform;
function Update ()
{
transform.position.z = Mathf.Lerp(transform.position.z, playerPrefab.position.z, 0.75);
transform.position.y = Mathf.Lerp(transform.position.y, playerPrefab.position.y, 0.75);
}
Player_spawn_script.js
var playerPrefab : Transform;
function Start ()
{
var playerInstance = Instantiate(playerPrefab, transform.position, Quaternion.identity);
var cameraScript : Camera_behaviour = gameObject.GetComponent(Camera_behaviour);
cameraScript.playerPrefab = playerInstance;
}