- Home /
Create a script to disable local gameobject and enable another gameobject in the scene
So I have a scene that starts out with a camera panning across the map. I have it set so when the user presses x, the gameobject that has the panning camera (the gameobject is named cinacam), will be disabled. How can I code my script to also activate first person controller as soon as the panning camera is disabled?
Answer by iwaldrop · Dec 24, 2013 at 06:07 AM
Since you didn't specify a language, I chose C# for you. You want to do something like this, although this is quite simple, it may be sufficient for your needs.
All you'd need to do is drag in your Player and Camera GameObjects into the fields in the inspector and hit play, then x to activate it. It will destroy itself or work repeatedly depending on how you set the destroyWhenDone flag.
using UnityEngine;
using System.Collections;
public class CameraSwitcher : MonoBehaviour
{
public GameObject cinaCam;
public GameObject player;
public bool destroyWhenDone = true;
void Update()
{
if (Input.GetKeyDown(KeyCode.X))
SwitchCameras();
}
void SwitchCameras()
{
cinaCam.SetActive(!cinaCam.activeSelf);
player.SetActive(!player.activeSelf);
if (destroyWhenDone)
Destroy(this);
}
}
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Can I attach the Main Camera to two scripts? 1 Answer
Change the background color attribute of a camera in C#? 2 Answers
Off center projection matrix does not move skybox 0 Answers
Activating & Deactivating prefabs from a prefab pool VS simply moving them on and off-camera (iOS) 1 Answer