Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by EwanMcEwan69 · May 06, 2020 at 10:30 AM · arraygameobjectsif-statements

How to compare GameObjects in an array?

I am quite new to Unity and I am making a program for my school project. It is doing to be a classic puzzle game style of matching specific items in a row in order clear them. I have figured how to clear the objects but my only problem is how to detect if two items next to each other are the same. I have used a 2D array which has stored each type of cloned prefab in its corresponding grid position but have found out using commands like:

 if (Array[x, y] == Array[x-1, y])

doesn't work for some reason.

Anyone have any ideas how i could overcome this it would be greatly appreciated.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by fblast1 · May 06, 2020 at 01:20 PM

Well, this checks the reference of each element if they refer to same gameobject https://docs.unity3d.com/ScriptReference/GameObject.html

if you need to check the element prefab type itself use Tags for each type you have and assign them then compare the tags itself https://docs.unity3d.com/Manual/Tags.html


Regards

Comment
Add comment · Show 3 · 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 Tario · May 06, 2020 at 01:35 PM 1
Share

Just to expand on this: in your described case of a matching game, tags are the way to go. Tags will not always be the right answer to comparing two objects though. They work best when you only have a few different types of object. Too many tags gets confusing.

As an alternative to Tags, you could check to see if both objects had the same components with GetComponent<> or you could ask if they had a specific component and then compare the values of that component to see if they're the same. It all depends on how complex your game is and how specific you want to be with comparing two things.

If you wanted to reduce the number of GetComponent<> calls, you could swap out your array from GameObject to the component you were comparing.

When you use ==, you're actually calling a method to compare the two things either side of the ==. It's possible to write your own version of this method and some objects in Unity have a custom method for comparing them already. By default though, it asks if the two things you're comparing are literally the same object; as in, "do they both occupy the same space in your RA$$anonymous$$?", hence your current problem. Each instance of your cloned prefab is a different thing, stored in a different place in RA$$anonymous$$.

avatar image EwanMcEwan69 · May 06, 2020 at 03:43 PM 0
Share

Thanks a lot using tags helped a lot as i only have 6 different items that need to be checked. This has helped me overcome a problem I have been stuck on for way too long.

avatar image fblast1 EwanMcEwan69 · May 07, 2020 at 01:49 AM 0
Share

Glad it helped you :)

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

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

As I compare 4 objects in an array? 2 Answers

Storing transforms from objects in Array 1 Answer

C# Array problems 2 Answers

comparing variables of gameobjects in an array 2 Answers

have an array that finds game objects with tag that is the root object/highest parent 2 Answers


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