Search Unity

Network toggle local camera

Discussion in 'Scripting' started by StegnerGames, Nov 21, 2017.

  1. StegnerGames

    StegnerGames

    Joined:
    Feb 26, 2017
    Posts:
    14
    How would I go about finding and toggling the camera attached to the local player on and off when networked? I have a script that toggles between 2 cameras, one is the player camera and the other is just a general one. It works when toggling to the general camera but doesn't toggle back.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Spectate : MonoBehaviour {
    6.  
    7.     public Camera Main_Cam;
    8.     public Camera Spectate_Cam;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.        
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.        
    18.     }
    19.  
    20.     public void Overview(){
    21.         if (Main_Cam) {
    22.             Main_Cam.enabled = false;
    23.             Spectate_Cam.enabled = true;
    24.         } else {
    25.             Spectate_Cam.enabled = false;
    26.             Main_Cam.enabled = true;
    27.         }
    28.     }
    29. }
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, is Main_Cam always a valid reference (not null)?
     
  3. StegnerGames

    StegnerGames

    Joined:
    Feb 26, 2017
    Posts:
    14
    Yeah, I thought I figured it out but now when I press the toggle button, it goes to 'No camera is rendering'

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Spectate : MonoBehaviour {
    6.  
    7.     private Camera Main_Cam;
    8.     public Camera Spectate_Cam;
    9.     private GameObject cam_main;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.      
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update () {
    18.      
    19.     }
    20.  
    21.     public void Overview(){
    22.         cam_main = GameObject.Find ("Player(Clone)/Main Camera");
    23.         Main_Cam = cam_main.GetComponent<Camera>();
    24.         if (Main_Cam.enabled == true) {
    25.             Main_Cam.enabled = (false);
    26.             Spectate_Cam.enabled = (true);
    27.         } else {
    28.             Spectate_Cam.enabled = (false);
    29.             Main_Cam.enabled = (true);
    30.         }
    31.     }
    32. }
    33.  
    Edit:
    Looks like it cant find the spectating camera.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, I'd suggest you get rid of game object find and just make the reference directly for now. Test it out, and see what happens. The logic seems correct, other than that.
     
  5. StegnerGames

    StegnerGames

    Joined:
    Feb 26, 2017
    Posts:
    14
    Got it working. Easy fix.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Spectate : MonoBehaviour {
    6.  
    7.     private Camera Main_Cam;
    8.     private Camera Spectate_Cam;
    9.     private GameObject cam_main;
    10.     private GameObject cam_spectate;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.      
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update () {
    19.      
    20.     }
    21.  
    22.     public void Overview(){
    23.         cam_spectate = GameObject.Find ("Spectate_Camera");
    24.         Spectate_Cam = cam_spectate.GetComponent<Camera> ();
    25.         cam_main = GameObject.Find ("Player(Clone)/Main Camera");
    26.         Main_Cam = cam_main.GetComponent<Camera> ();
    27.         if (Main_Cam.enabled == true) {
    28.             Main_Cam.enabled = (false);
    29.             Spectate_Cam.enabled = (true);
    30.         } else {
    31.             Spectate_Cam.enabled = (false);
    32.             Main_Cam.enabled = (true);
    33.         }
    34.     }
    35. }
    36.  
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool, you should try to reference them in another way (besides GameObject.Find) .. besides that, I'm glad you got it working :)