0
\$\begingroup\$

This is my code

using UnityEngine;

public class CompensarCamara : MonoBehaviour
{
    public Transform faceTransform;
    public float rotationSpeed = 10.0f;

    void Update()
    {
        // Rotar el canvas para que mire hacia la cámara
        transform.LookAt(faceTransform);

        // Obtener la rotación actual del canvas
        Quaternion currentRotation = transform.rotation;

        // Obtener la rotación horizontal de la cara
        float faceRotationY = faceTransform.rotation.eulerAngles.y;

        // Calcular la rotación de compensación
        Quaternion compensationRotation = Quaternion.Euler(0.0f, -faceRotationY, 0.0f);

        // Rotar el canvas en la dirección contraria a la rotación de la cara
        transform.rotation = currentRotation * compensationRotation;
    }
}

It is assigned to a canvas and what I want it to do is:

  1. Make the canvas always look at the camera
  2. Gimbal effect so that if the player rotates the camera the canvas doesn´t

The problem: It does the second thing but not the first one. It throws this error:

UnassignedReferenceException: The variable faceTransform of CompensarCamara has not been assigned.
You probably need to assign the faceTransform variable of the CompensarCamara script in the inspector.

Here is an image of the canvas in the inspector: enter image description here

It is obiously assigned or the gimbal effect would not work, any ideas what is happening here?

\$\endgroup\$
1
  • 2
    \$\begingroup\$ This is usually a symptom of having two instances of the script trying to run. The one you're looking at is set up correctly, but the other is not. You can add some code that checks if the variable is null, and if so, prints out the name of the object and any parent objects — this can help you find the stray copy of this script. \$\endgroup\$ Commented Apr 5, 2024 at 11:04

2 Answers 2

1
\$\begingroup\$

Single-click on the error message in the Unity Editor console, and the Editor will select the GameObject that the script is attached to in the Hierarchy panel. This way, you'll know what object has the script with no faceTransform reference.

\$\endgroup\$
0
\$\begingroup\$

Maybe it is not set in the prefab.

I had the same issue where I have set a reference for a game object in the inspector and it did not work, all I had to do was to go into the prefab and assign the values there.

\$\endgroup\$
1
  • \$\begingroup\$ A prefab should not get Update() calls unless another script is invoking it manually. \$\endgroup\$ Commented Apr 5, 2024 at 14:16

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.