thanks but I cant seem to get the function to work so implemented it in a different way. But, I am trying to enable and disable two different cameras but cannot get it to work. I get two errors
The name ‘GetComponent’ does not exist in current context
The type or namespace name ‘CamelotCamera’ could not be found. Are you missing a using directive or an assembly reference.
The code below:
{
Camera myCamera;
myCamera = GetComponenet<ExaliburCamera>(); //get current camera component
myCamera.enabled = false; //disable camera
myCamera = GetComponent<CamelotCamera>(); //get camera moving to component
myCamera.enabled = true; //enable camera
//function to clear whole cards in play(if its a single quest).
Turn = 3;
}
It appears that you are trying to assign the “myCamera” variable to components that are not the Camera component.
Here’s an example of what you should be assigning the “myCamera” variable to, if you are not assigning the variable within the inspector:
using UnityEngine;
//Declare the myCamera variable
Camera myCamera;;
void Start()
{
//Get the camera component on the gameobject
myCamera = gameObject.GetComponent<Camera>();
}
In order to toggle between two cameras, you must assign each Camera to separate variables, rather than the same one. Example:
using UnityEngine;
//Declare the camera variables. Assign these in the inspector, rather than the script.
Camera firstCamera;
Camera secondCamera;
//Use a bool to toggle between the active cameras.
bool firstCameraOn = false;
void Start()
{
//Do Nothing
}
void Update()
{
//If you press the tab button, switch cameras.
if(Input.GetKeyDown(KeyCode.Tab))
{
firstCameraOn = !firstCameraOn;
}
//Set the first camera to be active when firstCameraOn is true, and do the opposite for the second camera.
firstCamera.gameObject.SetActive(firstCameraOn);
secondCamera.gameObject.SetActive(!firstCameraOn);
}
thanks, it helps alot however I’m still getting an issue and I’m wondering if I am trying to accesses cameras wrong, the two cameras are called ExcaliburCamera and CamelotCamera.
Make sure that CurrCamera and CameraChoice1 are declaring Cameras and make sure that ExaliburCamera and CameloCamera are GameObjects, like below.
using UnityEngine;
using System;
public class Example : Monobehaviour
{
//Get these in the Start function
Camera CurrCamera;
Camera CameraChoice1;
//Set these in inspector
public GameObject ExaliburCamera;
public GameObject CamelotCamera;
void Start()
{
CurrCamera = ExaliburCamera.GetComponent<Camera>();
CameraChoice1 = CamelotCamera.GetComponent<Camera>();
}
}
okay, so it seems to work however when the current camera I’m using is disabled and the camera I’m moving to is enabled there is just a blank scene saying “scene is missing a full screen camera”
Do both cameras have camera components? Also, can you post your full code, please? This’ll help us find out if the problem is in the inspector or within the code.
You don’t appear to be assigning CurrCamera or CameraChoice1 to anything. Assuming you are assining the “ExaliburCamera” and “CamelotCamera” GameObjects in the inspector: try replacing this…
CurrCamera.gameObject.SetActive(false);//disable current camera
CameraChoice1.gameObject.SetActive(true);
sorry, I have solved the issue however this has confused me on something. When an object has a component that is unticked does this mean it is disabled or non-existent.