x


Replace GameObject with another GameObject

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

Well. My character "ObjChar" should be change in to another Object, Object "ObjExplosion"

If "ObjChar" is onTrigger with a rock, then "ObjeChar 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 27 47 53

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

1 answer: 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
89.7k 18 31 85

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

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

Explosion.gameObject.SetActive(true);

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

x3368
x1677
x70
x13

asked: Jun 11, 2013 at 10:15 PM

Seen: 1603 times

Last Updated: Jun 11, 2013 at 11:31 PM