Search Unity

When switching scenes some of my scripts stop working

Discussion in 'Editor & General Support' started by technano, Jun 1, 2020.

  1. technano

    technano

    Joined:
    Jan 13, 2016
    Posts:
    23
    So I have a camera follow script that I use in my main game scene. When I go from my main menu to that scene the script doesn't work. Even when I start Unity up on just that scene it doesn't work.

    The only fix right now is to remove the camera follow script component and add it back on. But obviously that won't work at runtime.

    I've looked around my scene and all my other scripts seem to be working fine except that one! Any help with this strange bug would be greatly appreciated!

    And In cause you need the code to the camera follow script here it is:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. namespace AL
    6. {
    7.      public class CameraHandler : MonoBehaviour
    8.      {
    9.          public Transform targetTransform;
    10.          public Transform cameraTransform;
    11.          public Transform cameraPivotTransform;
    12.          private Transform myTransform;
    13.          private Vector3 cameraTransformPosition;
    14.          private LayerMask ignorLayers;
    15.          private Vector3 cameraFollowVelocity = Vector3.zero;
    16.          public static CameraHandler singleton;
    17.          public float lookSpeed = 0.1f;
    18.          public float followSpeed = 0.1f;
    19.          public float pivotSpeed = 0.03f;
    20.          private float targetPosition;
    21.          public float defaultPosition;
    22.          private float lookAngle;
    23.          private float pivotAngle;
    24.          private float minimumPivot = -35f;
    25.          private float maximumPivot = 35f;
    26.          public float cameraSphereRadius = .2f;
    27.          public float cameraCollisionOffset = .2f;
    28.          public float minimumCollisionOffset = .2f;
    29.          private void Awake()
    30.          {
    31.              singleton = this;
    32.              myTransform = transform;
    33.              defaultPosition = cameraTransform.position.z;
    34.              ignorLayers = ~(1 << 8 | 1 << 9 | 1 << 10);
    35.          }
    36.          public void FollowTarget(float delta)
    37.          {
    38.              if (targetTransform != null)
    39.              {
    40.                  Vector3 targetPosition = Vector3.SmoothDamp(myTransform.position, targetTransform.position, ref cameraFollowVelocity, delta / followSpeed);
    41.                  myTransform.position = targetPosition;
    42.              }
    43.              HandleCameraCollision(delta);
    44.          }
    45.          public void HandleCameraRotation(float delta, float mouseXInput, float mouseYInput)
    46.          {
    47.              lookAngle += (mouseXInput * lookSpeed) / delta;
    48.              pivotAngle += (mouseYInput * pivotSpeed) / delta;
    49.              pivotAngle = Mathf.Clamp(pivotAngle, minimumPivot, maximumPivot);
    50.              Vector3 rotation = Vector3.zero;
    51.              rotation.y = lookAngle;
    52.              Quaternion targetRotation = Quaternion.Euler(rotation);
    53.              myTransform.rotation = targetRotation;
    54.              rotation = Vector3.zero;
    55.              rotation.x = pivotAngle;
    56.              targetRotation = Quaternion.Euler(rotation);
    57.              cameraPivotTransform.localRotation = targetRotation;
    58.          }
    59.          public void HandleCameraCollision(float delta)
    60.          {
    61.              targetPosition = defaultPosition;
    62.              RaycastHit hit;
    63.              Vector3 direction = cameraTransform.position - cameraPivotTransform.position;
    64.              direction.Normalize();
    65.              if (Physics.SphereCast(
    66.                  cameraPivotTransform.position,
    67.                  cameraSphereRadius,
    68.                  direction, out hit,Mathf.Abs(targetPosition),
    69.                  ignorLayers))
    70.              {
    71.                  float dis = Vector3.Distance(cameraPivotTransform.position, hit.point);
    72.                  targetPosition = -(dis - cameraCollisionOffset);
    73.              }
    74.              if (Mathf.Abs(targetPosition) < minimumCollisionOffset)
    75.              {
    76.                  targetPosition = -minimumCollisionOffset;
    77.              }
    78.              cameraTransformPosition.z = Mathf.Lerp(cameraTransform.localPosition.z, targetPosition, delta / 0.2f);
    79.              cameraTransform.localPosition = cameraTransformPosition;
    80.          }
    81.      }
    82. }
    If you need to know anything else please ask!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    Are you getting any errors?

    I'm noticing two things that are suspect:
    1. Your script has a bunch of Transform references: "targetTransform", "cameraTransform", "cameraPivotTransform". Are any of these referencing objects that might exist in one scene but not a new scene?
    2. This script doesn't use any MonoBehaviour frame-by-frame callbacks. No Update(), no LateUpdate() etc.. What's actually invoking the FollowTarget, HandleCameraRotation, HandleCameraCollision methods? Does the thing that's invoking these methods still exist when you load the new scene?
     
  3. technano

    technano

    Joined:
    Jan 13, 2016
    Posts:
    23
    I do have a separate script that's calling those functions, and I've checked through that all transform references still exist in the scene and they get populated in the script properly.

    I have discovered just now though that in my other script that i'm using to call all the functions is a null reference. So I think I'll look into this path!