- Home /
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.
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:
Each image should have this set up :
(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 :)
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.
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!
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?
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
Follow this Question
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