First Person Controller Creation and destruction on runtime
I am trying to add a First Person controller, originally from the standard unity assets, to a project and on runtime disable it on a button click. I tried gameObject.AddComponent
but it seems to give me an error. What would be the easiest way to switch from the main camera to First Person controller Camera?
Thanks in advance
Answer by Dudicus · Jul 05, 2016 at 05:46 PM
Make the Main Camera and First Person Camera. Then in script when you what to switch cameras disable the one you don't want to use by using:
//To activate Main Camera
mainCameraVariable.SetActive(true);
firstPersonCamera.SetActive(false);
//To activate First Person Camera
mainCameraVariable.SetActive(false);
firstPersonCamera.SetActive(true);
But make sure to replace mainCameraVariable and firstPersonCamera with the variable you are using to control each GameObject.
Hope I helped!
public class cameraSwich : $$anonymous$$onoBehaviour {
bool toggle;
public void changeCam()
{
GameObject.Find("View/FirstPersonCharacter").GetComponent<FirstPersonController>().enabled = false;
toggle = false;
if (toggle == true)
{
GameObject.Find("View/FirstPersonCharacter").GetComponent<FirstPersonController>().enabled = true;
}
}
}
This is what i have. I am trying to enable or disable / create or destroy whatever works better.
Thanks in advance