Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How can I activate a scene camera (VERY SIMPLE) C#

Discussion in 'Scripting' started by tallieke, Feb 21, 2016.

  1. tallieke

    tallieke

    Joined:
    Aug 17, 2014
    Posts:
    37
    Hey everyone, I know this is exetremely simple stuff, and I suck at coding but here is what I need help with.
    (I can probably explain it without having to insert code) I am making a fps game (in C#), and I have a scenecamera object that looks over the scene, but when i spawn in it is supposed to disappear (and it does) but when I die, it is supposed to come back because the player isn't supposed to respawn. my question is, what kind of line of code could I use to activate the scene camera.

    Any help is greatly appreciated :)
     
  2. grimofdoom

    grimofdoom

    Joined:
    Sep 6, 2013
    Posts:
    168
    If you need to enable or disable a GameObject (camera,cube,prefab..ect; the check box next to the name in the editor). You can use 'GameObject.SetActive(false)' or 'GameObject.SetAcctive(true)' to switch the GameObject on/off (I use this for my camera's in my current game for switching pause screen back to player view).

    http://docs.unity3d.com/ScriptReference/GameObject.SetActive.html
     
  3. tallieke

    tallieke

    Joined:
    Aug 17, 2014
    Posts:
    37

    Thank you for helping. But how can I specify that it is the camera object that needs to be spawned in. (This code is on my player not my camera)
     
  4. thewhiterose1

    thewhiterose1

    Joined:
    Sep 8, 2013
    Posts:
    18
    I think you can create a public variable in the script, initialising the type to GameObject and then drag your camera into that variable slot on the side :). (Inspector).

    Code (CSharp):
    1. public GameObject myCamera;
     
  5. tallieke

    tallieke

    Joined:
    Aug 17, 2014
    Posts:
    37

    So i tried what you said. And the camera still is'nt switching. Could it be because of this in my setup script

    Code (CSharp):
    1. void Start ()
    2.     {
    3.         if (!isLocalPlayer)
    4.         {
    5.             DisableComponents ();
    6.             AssignRemoteLayer ();
    7.  
    8.         }
    9.         else
    10.         {
    11.             sceneCamera = Camera.main;
    12.             if (sceneCamera != null)
    13.             {
    14.                 sceneCamera.gameObject.SetActive (false);
    15.             }
    16.         }