Toggling between characters

So I have the 2D Tutorial from the Unity website, and I’m trying to create a more simple toggle between characters. I’d like to press the “t” key and be able to toggle between lerpz and the spaceship (with no camera interaction. I have them both on screen at once, I don’t need the camera to follow either one.
Here is what I have:

var target : Spaceship;
var target2 : PlatformerController;

function Update () {
	if (Input.GetKeyDown("t")  target.canControl == false){
	target.canControl = true;
	target2.canControl = false;
	}
	
	if (Input.GetKeyDown("t")  target.canControl == true){
	target.canControl = false;
	target2.canControl = true;
	}
}

when I press “t” nothing happens. Though, when I set the second it statement to GetKeyDown(“y”) instead, then it works. Why can it not have “t” in both GetKeyDown statements?

Why would you ask “if (x)” twice anyway?

It’s “twice” as fast to go

if ( Input.GetKeyDown("T") ) {
  if ( canControl ) {
   // blah
  } else {
   // halb
  }
}
var target : Spaceship;
var target2 : PlatformerController;

function Update () {

	if (Input.GetKeyDown(KeyCode.T)){
	target.canControl = !target.canControl;
	target2.canControl = !target.canControl;
	}
}

Kinda Simpler.

Without more code its hard to see the problem but maybe
target.canControl
should be:
target.someScript.CanControl?

I think active is the way to go, then you cut off all scripts ect.

var cSwap : boolean;
var target : Spaceship; 
var target2 : PlatformerController; 

function Update () { 
   if (Input.GetKeyDown(KeyCode.t)){ 
   cSwap = !cSwap; 
   } 
   if(cSwap) {
   Spaceship.gameObject.active = true;
   PlatformerController.gameObject.active = false;
   }
   else if(cSwap==false) {
   Spaceship.gameObject.active = false;
   PlatformerController.gameObject.active = true;
   }
}

UnTested : and may need less code to work. I just like to make use of else if though lol. :smile:

PS Not sure if it will except the capital t in KeyCode. :idea:

You also cut off rendering, and that way has the “enormous” overhead of running two lines (.active =, .active =) every frame, no matter what. :stuck_out_tongue:

And
if ( true ) ; else ; is faster than if ( true ) ; else if ( !true ) ; since the first way only performs one comparison. :slight_smile:

I’m not sure what you mean by “active” and “cutting off all the scripts”, but I switched to using else and it did work. I should have thought of that to begin with, but I’m a scripting noob so I’m still getting used to how things work.

Thanks guys!