Replace GameObject with another GameObject

Guys, I got a problem with the replacement of an Object.

Well. My character “Obj_Char” should be change in to another Object, Object “Obj_Explosion”

If “Obj_Char” is onTrigger with a rock, then "Obje_Char should change in to “Obj_Explosion”. Someone can help me out with this?

I got this for my Rock JavaScript;

var CharExplode : GameObject;
var Explosion : GameObject;

function OnTriggerEnter(other : Collider)
{
if (other.gameObject.CompareTag("CharTag")) {
    CharExplode = Explosion;
}
}

But this doesn’t work. So please help :smiley:

Thanks in advance

I’m not sure what replacement means to you, nor have you described any parent/child relationship between these two game objects. Assuming the explosion game object is deactivated you might be what something like this:

#pragma strict

var charExplode : GameObject;
var explosion : GameObject;
 
function OnTriggerEnter(other : Collider) {
    if (other.gameObject.CompareTag("CharTag")) {
        charExplode.SetActive(false);
        explosion.transform.position = charExplode.transform.position;
        explosion.transform.rotation = charExplode.transform.rotation;
        explosion.SetActive(true);
    }
}

I also have the same need.
I would replace GameObject1 with GameObject2, not just repositioning GameObject2 and deactivate GameObject1.

Assume that I have a GameObject parent and GameObject1 is a child of the parent.
Then I want to switch GameObject1 with GameObject2, by code.

Is it possible?