I can't change the LookAt through code :(

I have a little Code where it’s drawing a line towards the closest enemy in the Scene Window.

Now I want the cinemachine camera to swap the LookAt (that is looking to the player) with the enemy by holding down the right mouse button, but I am getting these two Errors as if it can’t find the enemy.

Here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;

public class FindClosestEnemy : MonoBehaviour
{
    Cinemachine.CinemachineFreeLook freeLook;

    Enemy closestEnemy = null;
    public Transform playerLookAt;

    public void Start()
    {
        freeLook = GetComponent<Cinemachine.CinemachineFreeLook>();
    }


    void Update()
    {
        FindTheClosestEnemy();

        if (Input.GetMouseButtonDown(1))
        {
            freeLook.LookAt = closestEnemy.transform;
        }
        else if (Input.GetMouseButtonUp(1))
        {
            freeLook.LookAt = playerLookAt;
        }

        Debug.Log(closestEnemy);
    }

    public void FindTheClosestEnemy()
    {
        float distanceToClosestEnemy = Mathf.Infinity;
        Enemy[] allEnemies = GameObject.FindObjectsOfType<Enemy>();

        foreach (Enemy currentEnemy in allEnemies)
        {
            float distanceToEnemy = (currentEnemy.transform.position - this.transform.position).sqrMagnitude;
            if(distanceToEnemy < distanceToClosestEnemy)
            {
                distanceToClosestEnemy = distanceToEnemy;
                closestEnemy = currentEnemy;
            }
        }
        Debug.DrawLine(this.transform.position, closestEnemy.transform.position);
    }
}

The Debug.Log(closestEnemy); is even telling me which GameObject is the closest

but the Cinemachine is not swapping the LookAt with the Enemy

and these are the errors:

  1. NullReferenceException: Object reference not set to an instance of an object
    FindClosestEnemy.Update () (at Assets/New/FindClosestEnemy.cs:25)

  2. NullReferenceException: Object reference not set to an instance of an object
    FindClosestEnemy.Update () (at Assets/New/FindClosestEnemy.cs:29)

Edit: I set the Enemy in Line 11 to public and It also shows me in the inspector which the closest one is.

From the error message, maybe freeLook is null.

freeLook is set in Start() by GetComponent. Did you attach your script to a gameObject that has CinemachineFreelook?

Actually, no it’s in the players GameObject because I use 2 CinemachineFreeLooks (one for normal view and one for a closer view) I can transition them by enabling and disabling like this:

if (Input.GetMouseButton(1))
        {
            thirdPersonCam.SetActive(false);
            aimCam.SetActive(true);
        }
        else
        {
            thirdPersonCam.SetActive(true);
            aimCam.SetActive(false);
        }

I put this provisionally in the players movement script to test it out.

Edit: Could it be, that I get his Error, because the code does not know to which one it should give response? Because there are two but I didn’t assigned one specifically?

The script does not have a reference to any freelook. It expects it on the gameObject it is attached to.

Make freeLook public and set freeLook from the editor. Change your script to this:

public class FindClosestEnemy : MonoBehaviour
{
    public Cinemachine.CinemachineFreeLook freeLook; // set this in the editor
    Enemy closestEnemy = null;
    public Transform playerLookAt;
 
    void Update()
    {
// ...
1 Like

I set it before hitting play, it dissapears, I set it while play, it works perfect!

I changed the freeLook = GetComponent…

to

freeLook=GameObject.FindGameObjectWithTag(“Cinemachine”).GetComponent<Cinemachine.CinemachineFreeLook>();

and added a tag to it. now it works perfectly :smile: Thanks a LOT!