Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by Madix · Apr 08, 2015 at 09:57 AM · c#gameobjecttriggerevent

How to match 2 UI objects with Pointer Click?

Hello,

I'm still very much a beginner with Unity and am using Unity 5.0 currently.

I'm using a world space canvas that has a panel with a gird layout. The layout will hold 20 square images. The squares will be randomized in position and will have a pair (10 unique images). The goal is to click on the two matching images.

How can I compare the two images clicked to confirm they match then set them to invisible to avoid the grid layout group auto arranging the remaining images? I imagine it would be a combination of a script and the event trigger PointerClick? I'm new to coding and am using C# so I don't have a lot of experience. I believe I would need to create a public string in the script that would go on the prefab that would let me give the images a name and it would compare them the name in the string? Would someone be able to point me in the right direction and possible provide some example code or if they know of a tutorial video I'd be happy to watch. I haven't been able to find anything as of yet.

Thank you for your time and assistance.

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
2
Best Answer

Answer by Nerevar · Apr 08, 2015 at 01:28 PM

Hello,

I came up with this solution (using unity 4.6 but it should be close to your version, at least I hope):

So I work with 4 images in the example and I use their name to check if they Match

This is the hierarchie in my scene:

alt text

Each image should have this set up :

alt text

(You might need to add event->Event Trigger component and UI->selectable) I did it with colors but you can of course set the appropriate sprite to your image. The set-up can all be done from the inspector or via script depending on how you generate your board.

About event trigger: when you click on the image it is selected and it calls the function CheckMatch from GameHandler.cs (see below)

Then I have this script "GameHandler.cs" to check the match between the images (this script can be on any gameobject, in my scene its on camera):

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 using System.Collections.Generic;
 
 public class GameHandler : MonoBehaviour {
 
     public EventSystem mySystem;
     public List<Selectable> selectables;
     private GameObject lastSelectedObject;
 
     void Start(){
 
         selectables = Selectable.allSelectables;
 
     }
 
     public void CheckMatch() {
 
 
 
         if (lastSelectedObject == null)
             lastSelectedObject = mySystem.currentSelectedGameObject;
         Debug.Log ("Select & Checking Match..");
 
 
         if (mySystem.currentSelectedGameObject != lastSelectedObject) {
 
             //Debug.Log ("Last Selected : " + lastSelectedObject);
             //Debug.Log("Current Selected : " + mySystem.currentSelectedGameObject);
 
             if (mySystem.currentSelectedGameObject.name == lastSelectedObject.name){
 
                 Debug.Log ("Image Match !");
                 UpdateBoardAfterSuccess();
 
             }
             else{
 
                 Debug.Log ("Images Not Matching");
                 ResetBuffers();
 
             }
 
         }
 
     }
 
     void UpdateBoardAfterSuccess(){
 
         // I brutaly set the whole gameObject to unActive but you could just disable the image component
         lastSelectedObject.SetActive (false);
         mySystem.currentSelectedGameObject.SetActive (false);
         ResetBuffers ();
 
     }
 
     void ResetBuffers(){
 
         lastSelectedObject = null;
         mySystem.SetSelectedGameObject (null);
         
     }
 }


Hope it helps :)


hierarch.png (19.8 kB)
inspector.png (39.4 kB)
Comment
Add comment · Show 4 · 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 Madix · Apr 08, 2015 at 06:31 PM 1
Share

Hello, thanks for the reply and assistance! I'm running into a bit of a problem, maybe the difference between 4.6 and 5. I'm trying to follow the steps but it won't allow me to attach the camera holding the GameHandler script to the event trigger. Looks like I'm only able to drop prefabs and scripts in there. Any thoughts? Thank you again.

avatar image Madix · Apr 08, 2015 at 06:42 PM 1
Share

I see why I couldn't add the camera object. I was trying to modify my prefab. It looks like I can set it up in the Hierarchy but as soon as I make it a prefab it strips the camera object off of it. Seems to do that for any of the objects. However I think this sets me down the right path and gives me a way to tinker with it and tune it. Thank you for the help!

avatar image philipchristian · Mar 03, 2018 at 07:14 PM 0
Share

Hi @Nerevar I have a question, I want to have a corresponding game object to be setActive(true) when the Images are match. However, I can only put one GameObject to be set active to true, how can I code the argument/condition for every match images to show their corresponding game objects?

avatar image Nerevar philipchristian · Mar 05, 2018 at 11:01 AM 0
Share

I am not sure I understand what you are trying to do. I suggest you submit a new question for your problem. Also I am not active anymore on this forum, I don't have time to investigate on this topic anymore.

regards

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How does Unity handle GameObject selection in the Scene View? 1 Answer

Animation playing other object animation 0 Answers

Distribute terrain in zones 3 Answers

I need help with triggers 1 Answer

Is there an event/hook/trigger to sub to when a texture is applied to a gameObject in scene in editor mode? 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