x


Comparing game objects

I am trying to compare two GameObjects to see if they are the same:

GameObject foo;
void Start(){
    foo = Instantiate(SomeGameObject);
}
void CompareGO(GameObject bar){
    if(foo == bar)
    {
       DoSomethingElse();
    }  
}

This doesn't work, because since I am instantiating the GameObjects at runtime, bar might be called "GameObject (Clone)" and == seems to only compare the name value of the GameObjects. Short of manually setting the name of the GameObject, are there any easier ways to do this?

more ▼

asked Aug 10 '11 at 05:38 PM

Disaster gravatar image

Disaster
482 52 58 68

Hi, I'm in a similar problem. Beginner in Unity3D. I want to check the target position movement and load a win screen. This does not work. Could you please help me ?

targetpos= Player2.transform.position.

if(Player.transform.position == targetpos) { Debug.Log ("Came Here"); Application.LoadLevel("WinScreen"); }

Feb 25 at 03:23 PM mogambo

Hi @mogambo! I converted your reply to a comment. Please, don't post your question as an answer to another (if similar) question. You can make a new question if you want, which is more likely to get you replies.

Feb 25 at 03:25 PM RudyTheDev
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Hi I'm not sure what your exactly but your code should work if you want to compare to see if the objects are the same instance. If you want to check to see if they are of the same type(from the same prefab) try use Object.GetType to compare the type of the object. I'm not sure if it will work because I'm I don't fully understand how prefabs work but give it a try.

GameObject foo;
void Start(){
    foo = Instantiate(SomeGameObject);
}
void CompareGO(GameObject bar){
    if(foo.GetType() == bar.GetType())
    {
        DoSomethingElse();
    }   
}

Edit: You say that the when comparing the GameObjects it compares by name. Is this true? So it doesn't compare them by reference?

Edit2: I just tested the above code and it doesn't work. GetType just returns GameObject. If comparing 2 GameObjects does compare by name and you want to see if they are the same instance you could use the instanceID to check if they are the same.

GameObject foo;
void Start(){
    foo = Instantiate(SomeGameObject);
}
void CompareGO(GameObject bar){
    if(foo.GetInstanceID() == bar.GetInstanceID())
        DoSomethingElse();
    }   
}
more ▼

answered Aug 10 '11 at 06:05 PM

Blankzz gravatar image

Blankzz
276 19 20 23

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

It looks like it works if you do:

if(foo.name == bar.name)

more ▼

answered Jun 04 '12 at 04:14 AM

Hypothic gravatar image

Hypothic
0

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

x7803
x3071

asked: Aug 10 '11 at 05:38 PM

Seen: 4516 times

Last Updated: Feb 25 at 03:25 PM