Search Unity

Resolved Looking for how this script is called, 'Find References In Scene' isn't finding it

Discussion in 'Scripting' started by Carcophan, May 25, 2022.

  1. Carcophan

    Carcophan

    Joined:
    May 10, 2021
    Posts:
    79
    Hello everyone.

    Weird issue, but this is driving me nuts. I can't seem to find where or how this script is being called, but it is. I've done a million little things, and cobbled together Mirror tutorial scripts so many different times that I've forgotten where I even began or where to go next.

    I've searched around - and found and tried the 'right click on a script and click "Find References In Scene"' on all of the scenes I have (which is only 3, one loading, one play, and one old unused one), but this cameracontroller call is not found.


    The sample below was included in one of the Mirror tutorials, and is what is being used to follow the player. Yet it is not assigned to any of the objects, in any scene. But it is still working. How?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Mirror;
    5.  
    6. namespace UltimateCameraController.Cameras.Controllers
    7. {
    8.    
    9.     public class CameraController : MonoBehaviour
    10.     {
    11.        
    12.         private Camera targetCamera;
    13.  
    14.         [Header("Follow Settings")]
    15.         [Space(10)]
    16.  
    17.         [Tooltip("Should the camera follow the target?")]
    18.         public bool followTargetPosition = true; //Do we want our camera to follow the target object
    19.  
    20.         [Tooltip("The target object our camera should follow or orbit around")]
    21.         //public Transform targetObject; //The object that our camera should follow
    22.         public GameObject targetObject;
    23.  
    24.  
    25.         [Tooltip("The smooth factor when the camera follows a target object")]
    26.         [Range(0.2f, 1f)]
    27.         public float cameraFollowSmoothness; //The smooth factor when the camera follows a target object
    28.  
    29.         [Header("Orbit Settings")]
    30.         [Space(10)]
    31.  
    32.         [Tooltip("Should the player be able to orbit around the target object?")]
    33.         public bool orbitAroundTarget = true; //Do we want to add orbit functionality to the camera
    34.  
    35.         [Tooltip("The speed by which the camera rotates when orbiting")]
    36.         [Range(2f, 15f)]
    37.         public float rotationSpeed; //The speed by which the camera rotates when orbiting
    38.  
    39.         [Tooltip("The mouse button that the player must hold in order to orbit the camera")]
    40.         public MouseButtons mouseButton; //The mouse button that the player must hold in order to orbit the camera
    41.  
    42.         private Vector3 _cameraOffset; //How far away is the camera from the target
    43.  
    44.         private void Start()
    45.         {
    46.            // if (isLocalPlayer)
    47.             //{
    48.                 targetCamera = GetComponent<Camera>();
    49.                 _cameraOffset = targetObject.transform.position + new Vector3(-10.0f, 20.0f, -24.0f);
    50.  
    51.             //}
    52.         }
    53.  
    54.         //We use late update so that player movement is completed before we move the camera/This way we can avoid glitches
    55.         private void LateUpdate()
    56.         {
    57.  
    58.             //if (isLocalPlayer)
    59.             //{
    60.  
    61.                 //We do an error check
    62.                 if (targetObject == null)
    63.                 {
    64.                     Debug.LogError("Target Object is not assigned. Please assign a target object in the inspector.");
    65.                     return;
    66.                 }
    67.  
    68.                 var players = GameObject.FindGameObjectsWithTag("Player");
    69.  
    70.                 if (targetObject != null)
    71.                 {
    72.                     foreach (var player in players)
    73.                     {   //Debug.Log("IsNotNull5555");//works
    74.  
    75.                         targetObject = player;
    76.                         transform.LookAt(targetObject.transform.position);
    77.  
    78.                     }
    79.                 }
    80.  
    81.  
    82.                 //If we want the camera to follow the target
    83.                 if (followTargetPosition)
    84.                 {
    85.                     //We set the position the camera should move to, to the sum of the offset and the target's position
    86.                     var newPosition = targetObject.transform.position + _cameraOffset;
    87.                     //var newPosition = targetObject.position + _cameraOffset;
    88.                     transform.position = Vector3.Slerp(transform.position, newPosition, cameraFollowSmoothness);
    89.                 }
    90.  
    91.                 //If we want to make the player able to orbit around the target
    92.                 if (orbitAroundTarget)
    93.                 {
    94.                     //if (isLocalPlayer)
    95.                     //{
    96.                         //// We call the function to orbit the camera
    97.                         OrbitCamera();
    98.                    // }
    99.                 }
    100.             //}
    101.         }
    102.  
    103.         //Method to handle Orbit of the Camera
    104.         private void OrbitCamera()
    105.         {
    106.             //If the player holds the selected mouse button
    107.             if (Input.GetMouseButton((int)mouseButton))
    108.             {
    109.                 //We cache the mouse rotation values multiplied by the rotation speed
    110.                 float y_rotate = Input.GetAxis("Mouse X") * rotationSpeed;
    111.                 float x_rotate = Input.GetAxis("Mouse Y") * rotationSpeed;
    112.  
    113.                 //We calculate the rotation angles based on the cached values and a specific axes
    114.                 Quaternion xAngle = Quaternion.AngleAxis(y_rotate, Vector3.up);
    115.                 Quaternion yAngle = Quaternion.AngleAxis(x_rotate, Vector3.left);
    116.  
    117.                 //We multiply the rotation angle by the camera offset
    118.                 _cameraOffset = xAngle * _cameraOffset;
    119.                 _cameraOffset = yAngle * _cameraOffset;
    120.  
    121.                 // targetCamera.LookAt(targetObject.transform.position);
    122.                 transform.LookAt(targetObject.transform.position);
    123.  
    124.                 //We make our transform to "look" at the target      
    125.                 //////////transform.LookAt(targetObject.transform.position);
    126.                 //    transform.LookAt(targetObject);
    127.             }
    128.         }
    129.     }
    130.  
    131.     //Custom enumerator that represents the mouse buttons
    132.     public enum MouseButtons
    133.     {
    134.         LeftButton = 0,
    135.         RightButton = 1,
    136.         ScrollButton = 2
    137.     };
    138. }
    It is following the player, and rotating around the player - but the "drag to pan" feature isn't working, which is what started me down this rabbit hole.

    I'd like to know when/where/how this is triggered so I can work to resolve the mouse drag pan scenario.

    (or if anyone knows how to do that, that would cut out some steps)

    Any thoughts?

    Thanks all.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    While the game is running in the editor and the camera code is running, pause the game and type t:CameraController (don't forget the t:) in the hierarchy search bar, then you'll have your culprit.
     
    Kurt-Dekker likes this.
  3. Carcophan

    Carcophan

    Joined:
    May 10, 2021
    Posts:
    79
    Great tip, thank you Gro - but:

    upload_2022-5-25_9-16-12.png

    So the only other thing I can think of is that it is somehow baked into Mirror / Network code. But even then, wouldn't it still be visible here?

    The camera is certainly following the player... so strange.

    Just to show I did it right, I tested with a different script and it found it
    upload_2022-5-25_9-18-22.png
     
  4. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    602
    Since the code is doing
    targetCamera = GetComponent<Camera>();
    , surely this script must be attached to camera object. Maybe you couldn't find it because actual component attached to camera is subclass of the one you pasted.

    One more approach you can use to locate the corresponding game object is modifying that script and adding following code inside start or update methods.
    Code (CSharp):
    1. Debug.Log("text doesn't matter", gameObject);
    Once the debug log message is printed in console, clicking it should highlight the object in inspector. Having that debug message should also help you confirming that the corresponding script is really running instead of the camera movements you observed being caused by completely different script.
     
    GroZZleR likes this.
  5. Carcophan

    Carcophan

    Joined:
    May 10, 2021
    Posts:
    79
    I do feel like an idiot now. In my head, because I didn't add it myself (and I don't understand enough about this yet), I didn't realize the simplicty with what was needed to follow the player.

    There was a 'floatingInfo.transform.LookAt(Camera.main.transform);' hiding in a script, and the CameraController was indeed not in use as I thought. Which explains why I wasn't able to track down how it was being used :)

    I appreciate both of your times. Thank you so much for your help with my silly issue.
     
    GroZZleR likes this.