- Home /
iam trying toswitch between camares using keyboard keys using setactive function. but once the camera is inactive it is not active again
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class appear1 : MonoBehaviour { // Start is called before the first frame update void Start() { Update(); }
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("2"))
{
disappear();
}
if (Input.GetKeyDown("1"))
{
Appear();
}
}
void Appear()
{
gameObject.SetActive(true);
}
void disappear()
{
gameObject.SetActive(false);
}
}
Answer by yasssrenaeXD · Jul 26, 2019 at 07:10 PM
There is no need to call Update on Start since Update is called every frame. Try doing this.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class appear1 : MonoBehaviour {
public Camera cam;
// Start is called before the first frame update void Start() { cam.enabled=true; //assuming that camera is active on scene load }
// Update is called once per frame void Update() { if (Input.GetKeyDown("2")) { disappear(); } if (Input.GetKeyDown("1")) { Appear(); } } void Appear() { cam.enabled=true; } void disappear() { cam.enabled=false; } }