Before updating to Windows 10 you may need to return your Pro license. More information here

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 :D

Thanks in advance

more ▼

asked Jun 11, 2013 at 10:15 PM

avatar image

98 35 53 59

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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")) {
         explosion.transform.position = charExplode.transform.position;
         explosion.transform.rotation = charExplode.transform.rotation;
more ▼

answered Jun 11, 2013 at 10:47 PM

avatar image

115k 27 79 258

It says,

'position' is not a member of 'UnityEngine.GameObject'.


'rotation' is not a member of 'UnityEngine.GameObject'.

Jun 11, 2013 at 10:54 PM androids

Fixed. Usually I use Transform instead of GameObject for my linked variables.

Jun 11, 2013 at 10:55 PM robertbu

I got this right now, but nothing happens

 var CharExplode : Transform;
 var Explosion : Transform;
 function OnTriggerEnter(other : Collider){
     if (other.gameObject.CompareTag("CharTag")) {
         Explosion.position = CharExplode.position;
         Explosion.rotation = CharExplode.rotation;
Jun 11, 2013 at 11:02 PM androids

Note the code I posted doesn't "replace" anything. It puts one game object at the position of another, and disables the original.

  • Put a Debug.Log() statement to verify the code is being executed.

  • Look at the position in the Hierarchy to see if both game objects exist and are at the some position.

  • Make sure that 'explosion' is enables and visible.

Jun 11, 2013 at 11:18 PM robertbu

SetActive is a member of a GameObject. since you rewrote your code to use Transforms, you can do:


Note that variable names "should" be in lower case. The compiler does not care, but most experienced folks on this list use this convention, so using upper case makes your code harder to understand.

Jun 11, 2013 at 11:31 PM robertbu
(comments are locked)
10|3000 characters needed characters left

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?

more ▼

answered Aug 05, 2014 at 09:47 AM

avatar image

1 3 2 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Jun 11, 2013 at 10:15 PM

Seen: 3551 times

Last Updated: Aug 05, 2014 at 09:47 AM