Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by joan · Jul 19, 2014 at 11:13 PM · gamerandomcompare

How to compare if two gameObjects are the same?

I´m trying to make a game about visual memory. Random a game object appears and after 5 seconds disappear and another one appear. The goal is the player select that game object if is equal to the last one. How can i compare the fist game object and the one that players click? Any suggestions???

Thanks

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image joan · Jul 20, 2014 at 08:24 AM 0
Share

The main ideia is making a match game with graphic elements! It´s an idea thank's "book".

4 Replies

· Add your reply
  • Sort: 
avatar image
20

Answer by yacth_Mon · Apr 19, 2017 at 10:17 AM

Use Object.RefrenceEquals(obj1, obj2)

Like this

 if(GameObject.ReferenceEquals( firstGameObject, secondGameObject))
       Debug.Log("first and second are the same");

Also you can use to check if it null ? by changing secondGameObject to null.

Read more about ReferenceEquals

Hope this can help you. :)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fesul · May 06, 2017 at 10:37 PM 2
Share

I run the test on all 3 methods and this has been the fastest. $$anonymous$$uch faster than the others.

avatar image
8

Answer by 1Benisin · Jun 05, 2015 at 08:23 AM

Use GameObject.GetInstanceID()

 if (firstObj.GetInstanceID() == secondObj.GetInstanceID()) {
             // objects are the same instance
         }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Afassolas · Feb 28, 2016 at 07:36 AM -2
Share

Instance IDs are never the same

avatar image Ruri-Dev Afassolas · May 06, 2017 at 10:50 PM 2
Share

They'll be for the same object.

avatar image
5

Answer by Kiwasi · Jul 20, 2014 at 08:35 AM

... can't you use

 gameObject == otherGameObject

or am I missing something critical...

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ruri-Dev · May 06, 2017 at 11:56 PM 0
Share

I think it's ok, I trust in the GameObject's overriden comparison behaviour.

avatar image ytisfu · Apr 12, 2020 at 11:33 PM 0
Share

I can confirm this DOESN'T work - Unity doesn't recognize them as the same even when they are. In the end I gave the objects unique names and compared them later.

avatar image ETGgames ytisfu · May 17, 2020 at 03:11 PM 1
Share

You are RONG. the == operator is overloaded as such, meaning it correctly compares two objects with ==: alt text

whatsapp-image-2020-05-17-at-160000.jpeg (31.3 kB)
avatar image ytisfu ETGgames · May 21 at 03:04 PM 0
Share

At the end of the day it seems inconsistent whether the '==' is going to give the correct answer.

But comparing 2 different names/strings is a method that works %100 of the time in my experience. So I recommend it.

avatar image Mehrdad995 · May 21 at 02:39 PM 0
Share

It should work, other solutions are for the sake of performance.

avatar image
2

Answer by book · Jul 19, 2014 at 11:21 PM

You could maybe give a special name or tag to the different objects.

"Blue" for blue objects and "Red" for red etc.

Then in a script keep storing the name/tag of the last object that was on the screen in a string and then just do a check to see if the last object's tag was the same as the one that the user clicked.

Just an Idea..

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

32 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to compare if two gameObjects are the same? 2 Answers

Making a randomizer script for a basic "Runner" game... 1 Answer

How to make objects appear randomly around a spot 2 Answers

Random Time question 2 Answers

Randomly spawn a trigger area 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges