In Canvas, how do I require several triggers for a single event?
I am quite new to Unity and I feel that attempting to post any code might be a waste of our time. Basically, I am using Canvas to make a menu, and within that menu are several images that I want to have all be clicked on in order for a "secret menu item" to appear. I think I could get this working for a single item, but having multiple images to click has me lost. I have set up an Event Trigger with the EventTrigger.enabled function but I don't know how to translate that through code and with multiple clicks needing to be triggered in order for the secret button to appear. Any code or tips would be greatly appreciated. Thanks in advance.
Answer by Dibbie · Nov 20, 2016 at 10:20 PM
Easiest way to achieve what your looking to do:
Have your "secret menu items" either on a seperate Canvas, or simply under a seperate hierarchy within your 1 Canvas - for example, you can create a Panel, turn off the Image component of it so the panel isnt seen (or set the alpha color of it to 0), then child all your "secret menu items set 1" to that panel -- basically the point is, you want to group your menus you want to essentially toggle, under different sections of your hierarchy in your Canvas or in a new Canvas completely, but you NEVER want to put Transforms or other things that are not UI elements in your Canvas's because it can mess them up sometimes and not show the children on the UI anymore when visible.
Then, you want to write a script, something along the lines:
public GameObject[] hiddenMenus;
public void ShowMenuItems(int hiddenMenuIndex){
hiddenMenu.SetActive(true);
}
public void HideMenuItems(int hiddenMenuIndex){
hiddenMenu.SetActive(false);
}
Now, add a EventTrigger (click on your image you want to act as a "button" basically, click "Add Component", then go to "Event" (or type in the word "event"), then "Event Trigger") -- note youll have to do this one at a time, because EventTrigger doesnt support multi-editing (so you can select all your images and add the component, but you cant do anything with that component to all of them at once)
Then, click "Add New Event" or whatever the button says, and in your case, it sounds like youd want to add a "Pointer Click" event.
Leave the "Runtime only" line alone (this honestly really has no significant difference if you change it, you just dont need to worry about it in this case), then for the empty slot, you want to select the game object, that has this script you wrote on it -- you should really place this script on 1 object, like your main Canvas or your main Camera or something.
Now, set up your array on the object you placed the script on - select the little arrow for the array, give it a number, and then start setting the elements for each index of your array, to the game objects that hold the children of each of your hidden menus -- just remember the index or element number you put them at, so if you named your hidden menus in order by 1, 2, 3, and placed them in your array in that same order, itll be very easy to remember.
Then, go back to your image you want to act as a "button", the last section (the drop down) will become selectable, find your new script you wrote, and select the public function corresponding to what you want that button/image to do when clicked -- in that example, you probably want "ShowMenuItems", then set the number of what will be the 4th field, from 0, to whatever index you setup your menus on - this is where the memory comes in.
Once all that is done, if done correctly, you will be able to show different menus on different images you click on -- then you can apply the "HideMenuItems" as well to hide certain menus when you click on different images if youd so like.
Kind of a long-ish process that can be very confusing at first, but doing it in a "Event Trigger" way for each image, through one script with an array, is the easiest way I can think of doing what you want to do without over complicating the concept too much.
Hope this helped.
First off, really appreciate the detailed response. It is kinda confusing, but considering it is the best answer if have been able to find online yet, I will go head first with attempting it. ;)
No problem, if you run into issues and cant figure it out, your welcome to email me or drop a reply, and ill see if I can get some visuals explaining the process to help you out.
Do you know what could be causing this:
Assets/$$anonymous$$ain$$anonymous$$enu/Scripts/hidden$$anonymous$$enu.cs(9,28): error CS0117: hidden$$anonymous$$enu' does not contain a definition for
SetActive'
?
Sounds like you called your script "hidden$$anonymous$$enu", and a variable also the exact same name, so its confused and basically trying to tell you that your script "hidden$$anonymous$$enu" (which is what it thinks your trying to use SetActive on), cant be done (because scripts dont use SetActive) -- so youd maybe want to name your variable different then the script name.
Wow, I'm a noob. I did that now I get:
Assets/$$anonymous$$ain$$anonymous$$enu/Scripts/secret$$anonymous$$enu.cs(9,25): error CS0103: The name `hidden$$anonymous$$enu' does not exist in the current context
and the same for line 12.
I'm sorry, if I could figure this out I would leave you alone :D
Any way you do give visuals to help? You would be the actual best.
Your answer
Follow this Question
Related Questions
Trigger events with bullets 0 Answers
Unity Event that returns mouse over 0 Answers
UI event with non-instantiated Prefab as event handler. What happens? 2 Answers
OnpointerClick not working(parents gameObject has EventTrigger) 0 Answers
[Solved] Scroll not working when elements inside have click events 3 Answers