Search Unity

Cant disable first person controller C# Unity 5

Discussion in 'Scripting' started by Savvy001, May 22, 2015.

  1. Savvy001

    Savvy001

    Joined:
    Dec 27, 2012
    Posts:
    1
    can anyone see what i am doing wrong?
    I am trying to disable the first person controller.
    My script has the name space, it has a reference to the object with the firstpersoncontroller script on it.
    But i keep getting the error:
    NullReferenceException: Object reference not set to an instance of an object
    SwitchCameraPosition.Update () (at Assets/Main Menu/SwitchCameraPosition.cs:48)

    The error is strange to me, because the reference to the cubeCameraSwitch "is used" correct in the code's transform section.

    Hope u can help :)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityStandardAssets.Characters.FirstPerson;
    4.  
    5. public class SwitchCameraPosition: MonoBehaviour
    6. {
    7.     private bool switchViewA;
    8.     private bool switchViewB;
    9.     public float XPosOld;
    10.     public float YPosOld;
    11.     public float ZPosOld;
    12.     public float XPosNew;
    13.     public float YPosNew;
    14.     public float ZPosNew;
    15.     public Transform target;  
    16.     public float smoothing = 10000f;        
    17.     Vector3 offset;                    
    18.  
    19.     private GameObject cubeCameraSwitch;
    20.     private GameObject player;
    21.  
    22.     void Start ()
    23.     {
    24.  
    25.         switchViewB = true;
    26.         switchViewA = false;
    27.         cubeCameraSwitch = GameObject.FindGameObjectWithTag("CubeCameraSwitch");
    28.         player = GameObject.FindGameObjectWithTag("Player");
    29.    
    30.         offset = transform.position - target.position;
    31.  
    32.     }
    33.    
    34.    
    35.     void Update ()
    36.     {
    37.  
    38.        
    39.         if (Input.GetButton ("SwitchView"))
    40.         {
    41.             switchViewB = false;
    42.             switchViewA = true;
    43.             cubeCameraSwitch.GetComponent<FirstPersonController>().enabled = false;
    44.            
    45.         }
    46.  
    47.         if (switchViewA == true)
    48.         {
    49.             cubeCameraSwitch.transform.localPosition = new Vector3 (XPosNew ,player.transform.position.y + YPosNew,+ ZPosNew);
    50.             transform.localEulerAngles = new Vector3 (0,0,0);
    51.             Debug.Log ("Switched To First Person View");
    52.             offset = transform.position;
    53.             switchViewA = false;
    54.  
    55.    
    56.         }
    57.        
    58.         if (Input.GetButton ("Fire1"))
    59.         {
    60.             switchViewA = false;
    61.             switchViewB = true;
    62.             cubeCameraSwitch.GetComponent<FirstPersonController>().enabled = true;
    63.         }
    64.  
    65.         if (switchViewB == true)
    66.         {
    67.             cubeCameraSwitch.transform.localPosition = new Vector3 (XPosOld ,player.transform.position.y + YPosOld ,ZPosOld);
    68.             transform.localEulerAngles = new Vector3 (30,0,0);
    69.             Debug.Log ("Switched To God View");
    70.             offset = transform.position;
    71.             switchViewB = false;
    72.  
    73.         }
    74.         if (switchViewA == false && switchViewB == false)
    75.         {
    76.             // Calculate the initial offset.
    77.             //offset = transform.position - target.position;
    78.             // Create a postion the camera is aiming for based on the offset from the target.
    79.             Vector3 targetCamPos = target.position + offset;
    80.        
    81.             // Smoothly interpolate between the camera's current position and it's target position.
    82.             transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing * Time.deltaTime);
    83.         }
    84.     }
    85.  
    86.    
    87.  
    88.  
    89. }
    90.