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): using System.Collections; using System.Collections.Generic; using UnityEngine; public class Spectate : MonoBehaviour { public Camera Main_Cam; public Camera Spectate_Cam; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } public void Overview(){ if (Main_Cam) { Main_Cam.enabled = false; Spectate_Cam.enabled = true; } else { Spectate_Cam.enabled = false; Main_Cam.enabled = true; } } }
Yeah, I thought I figured it out but now when I press the toggle button, it goes to 'No camera is rendering' Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class Spectate : MonoBehaviour { private Camera Main_Cam; public Camera Spectate_Cam; private GameObject cam_main; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } public void Overview(){ cam_main = GameObject.Find ("Player(Clone)/Main Camera"); Main_Cam = cam_main.GetComponent<Camera>(); if (Main_Cam.enabled == true) { Main_Cam.enabled = (false); Spectate_Cam.enabled = (true); } else { Spectate_Cam.enabled = (false); Main_Cam.enabled = (true); } } } Edit: Looks like it cant find the spectating camera.
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.
Got it working. Easy fix. Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class Spectate : MonoBehaviour { private Camera Main_Cam; private Camera Spectate_Cam; private GameObject cam_main; private GameObject cam_spectate; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } public void Overview(){ cam_spectate = GameObject.Find ("Spectate_Camera"); Spectate_Cam = cam_spectate.GetComponent<Camera> (); cam_main = GameObject.Find ("Player(Clone)/Main Camera"); Main_Cam = cam_main.GetComponent<Camera> (); if (Main_Cam.enabled == true) { Main_Cam.enabled = (false); Spectate_Cam.enabled = (true); } else { Spectate_Cam.enabled = (false); Main_Cam.enabled = (true); } } }
Cool, you should try to reference them in another way (besides GameObject.Find) .. besides that, I'm glad you got it working