Script that switches between first and third person controller

Hello - in my game I switch between first and third person. When the third person collides with a certain trigger - the third person controller is destroyed, and a new one is instantiated.

The problem I have is that once the new controller is instantiated, this script no longer works - switching between first and third person. (as of course I only assigned the first third person controller and camera in the hierarchy)

Is there a way of changing the script so that rather than assigning the third person controller and camera in the hierarchy, the script just references whichever third person controller is currently active?

(Sorry I’m not sure how exactly to phrase it - I’m still very new to scripting)

This is the script I’m using to switch between characters (JavaScript):

#pragma strict

  	var cam01 : GameObject; // first person camera
    var cam02 : GameObject; // third person camera
    var player01 : GameObject; //first person controller
    var player02 : GameObject; //third person controller
    var check;                 // New check-variable
    //start with first person active
    function Start() { = true; = false; = false;
       check = true;
    function Update() {
    player01.transform.position = player02.transform.position;
     if (Input.GetKeyDown ("return")) {
       if(check) { = false;  = true;  = false; = true;
       else { = true;  = false;  = true; = false;
    check = !check;

Thanks very much, Laurien

Not really like this you would do it more like this:

 var prefab : Transform;
var script : SwitchCharacters; 
var playerGO : GameObject; //<--Should be filled with the Gameobject the SwitchCharacters-Script is attached to.
private var hasPlayed = false;
function OnTriggerEnter () {
var pos : Vector3;
if (!hasPlayed){
var newprefab = Instantiate (prefab);
script = playerGO.GetComponent(SwitchCharacters);
script.player02 = newprefab;

hasPlayed = true;

Depending on where the instantiating script is attached to you can maybe replace the playerGO with something like transform, transform.parent, … Whatever works in your case.

I you want to switch between tps and fps. Create a static var fps and set its bool value to true. Now whatever you want to do in fps, do it in a if condition if fps is true and else if it is false do what you want to do in fps. If the trigger event happens, check tha value of var fps. If its true set it to false, else it is false, set it to true. Hope u understood. I will get u the script in a few days.

This is very easy. All you need is a 3rd person controller with the camera where the head is, so as to look first person, and just move the camera back however you want: If you want to move it instantly, so it switches right away; if you want to scroll out to zoom out. You can just set up some sort of line that the camera moves on.