What I’m doing is loading my Player Character as a prefab which then has the ability to instantiate a 2nd prefab that is supposed to be able assume control while the player character is locked in place.
The prefab instantiated by the player character and the player character has a camera (nested) attached to them. I’m trying to slect the prefab’s camera so that when the player is controlling the prefab te prefab’s camera is active as the main camera.
The problem I’m running into is that I can’t figure out how to refer to the prefab’s camera to activate or deactivate it.
“gameObject.GetComponent” only refers to the components of the prefab and not to the nested camera so I’m lost.
Here’s the script as I have it now…
public class PlayerController : MonoBehaviour {
public float rotateSpeed;
public float forwardSpeed;
public float jumpHeight;
public Transform Armana;
public Camera MainCamera;
private Camera ArmanaCam;
private CharacterController playerController;
private bool PlayerActive = true;
// Use this for initialization
void Start () {
playerController = GetComponent<CharacterController> ();
MainCamera.enabled = true;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.Q)) {
if (PlayerActive) {
PlayerActive = false;
Armana.GetComponent<PMonsterController>().enabled = true;
MainCamera.enabled = false;
MainCamera.GetComponent <AudioListener>().enabled = false;
ArmanaCam.enabled = true;
ArmanaCam.GetComponent <AudioListener>().enabled = true;
} else {
PlayerActive = true;
Armana.GetComponent<PMonsterController>().enabled = false;
ArmanaCam.enabled = false;
ArmanaCam.GetComponent <AudioListener>().enabled = false;
MainCamera.enabled = true;
MainCamera.GetComponent <AudioListener>().enabled = true;
}
}
if (Input.GetKeyDown (KeyCode.E)) {
Instantiate (Armana, transform.position, transform.rotation);
//Assaign ArmanaCam... somehow
ArmanaCam.enabled = false;
ArmanaCam.GetComponent <AudioListener>().enabled = false;
}
if (PlayerActive) {
if (Input.GetButton ("Jump") && playerController.isGrounded) {
playerController.Move (Vector3.up * jumpHeight);
}
transform.Rotate (0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
Vector3 forward = transform.TransformDirection (Vector3.forward);
float speed = forwardSpeed * Input.GetAxis ("Vertical");
playerController.SimpleMove (speed * forward);
}
}
}