I am trying to make a camera switch between first person and third person. Here is the code:
[SerializeField]
private Camera ThirdPersonCam;
private KeyCode switchKey;
//Assingables
public Transform playerCam;
public Transform orientation;
private void Update()
{
MyInput();
Look();
if (Input.GetKeyDown(switchKey))
{
playerCam.enabled = !playerCam.enabled;
ThirdPersonCam.enabled = !ThirdPersonCam.enabled;
}
}
However, in this line playerCam.enabled = !playerCam.enabled;
, it shows this error:
'Transform' does not contain a definition for 'enabled' and no accessible extension method 'enabled' accepting a first argument of type 'Transform' could be found (are you missing a using directive or an assembly reference?
But in the line beneath it, it doesn’t show. I am confused why is it happening and enabled
is a definition for the Transform
. Can someone help me out to solve it.
Thanks.
CodePudding user response:
There is no "enable" for Transform in the documentation as far as I can tell: https://docs.unity3d.com/ScriptReference/Transform.html If you want to invert the active status (aka enable) from having a reference to their transform, use:
playerCam.gameObject.SetActive(!playerCam.gameObject.activeSelf)
Since this is a bit convoluted, it often makes sense to keep references to the game objects themselves instead of their transform.
CodePudding user response:
you've defined the global/serializable playerCam to be of type transform so you cannot enable/disable them. Change the types to Camera and it should be fine
[SerializeField]
private Camera ThirdPersonCam;
private KeyCode switchKey;
//Assingables
public Camera playerCam;
public Transform orientation;