Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug I can't change the LookAt through code :(

Discussion in 'Cinemachine' started by IsakuStudios, Aug 26, 2022.

  1. IsakuStudios

    IsakuStudios

    Joined:
    Jun 28, 2020
    Posts:
    7
    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:
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using Cinemachine;
    6.  
    7. public class FindClosestEnemy : MonoBehaviour
    8. {
    9.     Cinemachine.CinemachineFreeLook freeLook;
    10.  
    11.     Enemy closestEnemy = null;
    12.     public Transform playerLookAt;
    13.  
    14.     public void Start()
    15.     {
    16.         freeLook = GetComponent<Cinemachine.CinemachineFreeLook>();
    17.     }
    18.  
    19.  
    20.     void Update()
    21.     {
    22.         FindTheClosestEnemy();
    23.  
    24.         if (Input.GetMouseButtonDown(1))
    25.         {
    26.             freeLook.LookAt = closestEnemy.transform;
    27.         }
    28.         else if (Input.GetMouseButtonUp(1))
    29.         {
    30.             freeLook.LookAt = playerLookAt;
    31.         }
    32.  
    33.         Debug.Log(closestEnemy);
    34.     }
    35.  
    36.     public void FindTheClosestEnemy()
    37.     {
    38.         float distanceToClosestEnemy = Mathf.Infinity;
    39.         Enemy[] allEnemies = GameObject.FindObjectsOfType<Enemy>();
    40.  
    41.         foreach (Enemy currentEnemy in allEnemies)
    42.         {
    43.             float distanceToEnemy = (currentEnemy.transform.position - this.transform.position).sqrMagnitude;
    44.             if(distanceToEnemy < distanceToClosestEnemy)
    45.             {
    46.                 distanceToClosestEnemy = distanceToEnemy;
    47.                 closestEnemy = currentEnemy;
    48.             }
    49.         }
    50.         Debug.DrawLine(this.transform.position, closestEnemy.transform.position);
    51.     }
    52. }
    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.
     
    Last edited: Aug 26, 2022
  2. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    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?
     
  3. IsakuStudios

    IsakuStudios

    Joined:
    Jun 28, 2020
    Posts:
    7

    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:

    Code (CSharp):
    1.  
    2. if (Input.GetMouseButton(1))
    3.         {
    4.             thirdPersonCam.SetActive(false);
    5.             aimCam.SetActive(true);
    6.         }
    7.         else
    8.         {
    9.             thirdPersonCam.SetActive(true);
    10.             aimCam.SetActive(false);
    11.         }
    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?
     
  4. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    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:
    Code (CSharp):
    1. public class FindClosestEnemy : MonoBehaviour
    2. {
    3.     public Cinemachine.CinemachineFreeLook freeLook; // set this in the editor
    4.     Enemy closestEnemy = null;
    5.     public Transform playerLookAt;
    6.  
    7.     void Update()
    8.     {
    9. // ...
     
    IsakuStudios likes this.
  5. IsakuStudios

    IsakuStudios

    Joined:
    Jun 28, 2020
    Posts:
    7

    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 :D Thanks a LOT!