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

camera switching problem

Discussion in 'Scripting' started by mael_elseware, Oct 23, 2014.

  1. mael_elseware

    mael_elseware

    Joined:
    Aug 26, 2013
    Posts:
    3
    Hello guys !
    Am pretty new to unity programming and got stuck lately by an annoying bug. Searched a lot on the internet but maybe I am too stupid to understand what I am doing wrong or maybe it's just not in there. (Pity, help me)

    So here is the things. I am using a UFPS camera to travel in my scene and a second camera, not moving at all, to display a clickable UI.
    So i put a script on a wall for instance, when the guy click on the wall, it switches camera and show him some buttons to change the wall textures.

    My bug is that, when I click on the wall it says me this :
    NullReferenceException: Object reference not set to an instance of an object
    Canvas_Activer.Start () (at Assets/Scripts/Canvas_Activer.cs:24)
    (canvas activer being my script)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Canvas_Activer : MonoBehaviour {
    5.  
    6.     public string CanvasName;
    7.     public int Timer;
    8.     public float Speed=1.0f;
    9.  
    10.     private GameObject Canvas;
    11.     private Canvas CanvasComponent;
    12.     private GameObject Maincamera;
    13.     private Camera MovingCamera;
    14.     private float timing=0.0f;
    15.     private bool flag=false;
    16.     private float startTimer;
    17.    
    18.  
    19.     // Use this for initialization
    20.     void Start () {
    21.         GameObject Canvas = GameObject.Find(CanvasName);
    22.         GameObject Maincamera = GameObject.Find("FPSCamera");
    23.         CanvasComponent = Canvas.GetComponent<Canvas>();
    24.         MovingCamera = Maincamera.GetComponent<Camera>() as Camera;
    25.     }
    26.    
    27.     void Update () {
    28.         if(flag == true){
    29.             timing = Time.time - startTimer;
    30.             Screen.showCursor = true;
    31.             Debug.Log(timing);
    32.             Maincamera.camera.enabled=false;
    33.  
    34.             if(timing>Timer){
    35.                 CanvasComponent.enabled=false;
    36.                 Screen.showCursor = false;
    37.                 flag = false;
    38.             }
    39.         }
    40.     }
    41.    
    42.  
    43.     void OnMouseOver(){
    44.         Screen.showCursor = true;
    45.         if(Input.GetButton("Fire1")){
    46.             CanvasComponent.enabled=true;
    47.             startTimer = Time.time;
    48.             flag = true;
    49.         }
    50.     }
    51.  
    52.     }
    53.  
    54.  
    55.  
    Tryed out a lot of things but... just can't do it :(
    If someone could help me, I would really be gratefull

    Thanks all !
     
  2. mael_elseware

    mael_elseware

    Joined:
    Aug 26, 2013
    Posts:
    3
    AM really struggling with this.