How do I use a power up to change the character I'm controlling?

Here is my situation:

  1. I have a particle system with a box collider attached with “is trigger” checked.
  2. I have 2 playable characters with character controllers attached.

When you start the game you start out as “character 1” which has the tag: “Player.”

When the character runs through the “powerup” which is the particle system listed above (#1) I want him to change into character #2 which has the tag: “Player 2.”

I’m new to Unity3d and I could have this totally wrong… but I have the following script attached to the particle system prefab I created:

var redapple : GameObject;

var yellowapple : GameObject;

function OnTriggerEnter(other : Collider){

if(other.gameObject.tag == "Yellow Powerup"){
   Destroy(other.gameObject); 
   redapple = GameObject.FindWithTag("Player");
   yellowapple = GameObject.FindWithTag("Player2"); 

redapple.active = false;

yellowapple.active = true;

}

}

Any ideas guys? I’ve been trying to figure this out for so long now… appreciate any help. Thanks!

Since I’m primarily a C# guy, I’m not going to make a code fragment to demonstrate this, but it should send you in the right direction. Avoid using Destroy() if you can. It is costly, and you may want to return to the “redapple” object later. Instead, have two (2) GameObjects that hold redapple and yellowapple, and make them the children of a “playercontroller” GameObject. When the level loads, make sure to start things off with redapple.renderer.enabled = true, and yellowapple.renderer.enabled = false. When the playercontroller object enters the trigger, just set redapple’s renderer.enabled to false, and the opposite for yellowapple.

I don’t know what the apple meshes look like, but if they are the same mesh and only color is changing, you may want to consider sticking with a single “apple” GameObject and simply change the material/texture.

It looks like you put a component on the Particle System looking for when it collides with the Particle System… Did you mean to put this on the character instead? Otherwise the Particle System should be looking for a collision with the character.

var redapple : GameObject;
var yellowapple : GameObject;

function OnTriggerEnter(other : Collider){

   if(other.gameObject.tag == "Player"){
      Destroy(this.gameObject); 
      redapple = GameObject.FindWithTag("Player");
      yellowapple = GameObject.FindWithTag("Player2"); 

      redapple.active = false;
      yellowapple.active = true;
   }
}

Ok, thanks to the above advice and a lot of research I finally got it working correctly! Here is the code in Java in case anyone else is looking to do this:

Create a script and attach it to the POWERUP:

private var yellowapple : GameObject;

private var redapple: GameObject;

function Awake () {

yellowapple = GameObject.FindWithTag(“Player2”);

redapple = GameObject.FindWithTag(“Player”);

yellowapple.SetActiveRecursively(false);

redapple.SetActiveRecursively(true);

}

function OnTriggerEnter(other : Collider){

yellowapple.SetActiveRecursively(true);

redapple.SetActiveRecursively(false);

}