I want to enable/disable script on my Camera on Key or UIButton press.
using UnityEngine;
using System.Collections;
public class Changer1 : MonoBehaviour {
private Camera myCamera;
void Start () {
myCamera = GetComponent<NameOfMyScript> ();
}
// Update is called once per frame
void Update () {
if(Input.GetKeyUp(KeyCode.Space))
{
myCamera.enabled = false;
}
}
}
I am getting an error CS0029: Cannot implicitly convert type ‘NameOfMyScript’ to 'UnityEngine.Camera
If you want to enable/disable a self written script u will have to use another variable type than Camera, since your variable ‘myCamera’ is currently only able to store Camera-components.
Try the following and be sure to swap every ‘NameOfMyScript’ with the actual name of your script.
using UnityEngine;
using System.Collections;
public class Changer1 : MonoBehaviour {
private NameOfMyScript myScript;
void Start () {
myScript= GetComponent<NameOfMyScript> ();
}
// Update is called once per frame
void Update () {
if(Input.GetKeyUp(KeyCode.Space))
{
myScript.enabled = false;
}
}
}
This should fix the error but still won’t pose the toggle functionalty you asked for.
For that you have to replace your if statement with