in my unity game I'm creating a Menu and and I want to move the camera with the player, when the game is started and I want to move the camera in the right position before (Vector3(1, 1, -1)). The problem is that I am using a Button connected with a script setting a public bool variable. But whatever I do, the NullReferenceException Error appears.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StartPosition : MonoBehaviour
{
public bool cameraFollow;
public void TakeStartPosition()
{
transform.position = new Vector3(1, 1, -1);
cameraFollow = true;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform playerTransform;
public StartPosition followOn;
private Vector3 cameraOffset;
[Range(0.01f, 1.0f)]
public float smoothFactor = 0.5f;
// Start is called before the first frame update
void Start()
{
if (followOn.cameraFollow)
{
cameraOffset = transform.position - playerTransform.position;
Debug.Log("It must work!!!");
}
}
// Update is called once per frame
private void LateUpdate()
{
if (followOn.cameraFollow)
{
Vector3 newPos = playerTransform.position + cameraOffset;
newPos.z = transform.position.z;
transform.position = Vector3.Slerp(transform.position, newPos, smoothFactor);
}
}
}