x


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

androids gravatar image

androids
66 32 52 57

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

answered Jun 11, 2013 at 10:47 PM

robertbu gravatar image

robertbu
104k 19 36 105

It says,

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

and

'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

Unity gives me an error:

'SetActive' is not a member of 'UnityEngine.Transform'

Jun 11, 2013 at 11:23 PM androids
(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 at 09:47 AM

MRCty gravatar image

MRCty
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

By RSS:

Answers

Answers and Comments

Topics:

x3640
x1757
x76
x13

asked: Jun 11, 2013 at 10:15 PM

Seen: 1984 times

Last Updated: Aug 05 at 09:47 AM