Apologies for my poor wording in advance. I’m trying to script functions for a UI tab system. When I click a button within the tab system, it opens a dropdown menu with buttons that open to their own pages. However, if I were to click on the same button within the tab system to close the dropdown menu, the page(s) opened by the buttons within the dropdown menu remain open. I want it so that when I click the button within the tab system to close the dropdown menu, it will also close the corresponding pages.
Here is the script I have so far-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Drop_down_tab : MonoBehaviour
{
public Button tabButton;
public GameObject panel;
public GameObject child;
void Start()
{
Button btn = tabButton.GetComponent<Button>();
btn.onClick.AddListener(TaskOnClick);
}
void TaskOnClick()
{
panel.SetActive(!panel.activeSelf);
}
void update()
{
if (panel.active(false))
{
child.SetActive(!child.activeSelf);
}
}
}
Well, not sure if this will give you the behavior you want, since I didn’t really understand what you’re trying to do. However, you must remember something important.
Unity “magic methods” such as Start, Awake, Update, etc must always be written correctly for Unity to call it automatically. So, spelling, CAPS, and parameters.
What Brath says also applies to everything in C#… and in Unity there is an additional requirement that MonoBehaviour-derived classes perfectly match their filenames. If your filename is anything except Drop_down_tab.cs, you will encounter a bunch of other editor inspector issues.
Generally, this is how you should think about hammering in code you find on the interwebs:
How to do tutorials properly:
Tutorials are a GREAT idea. Tutorials should be used this way:
Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That’s how software engineering works. Every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly. Fortunately this is the easiest part to get right. Be a robot. Don’t make any mistakes. BE PERFECT IN EVERYTHING YOU DO HERE.
If you get any errors, learn how to read the error code and fix it. Google is your friend here. Do NOT continue until you fix the error. The error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.
Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.
Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost.
Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there’s an error, you will NEVER be the first guy to find it.
Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
Since I apparently need to state it, it is because of following tutorials that I am posting on here. The ones I have been able to find that touch upon the functions I am trying to learn either gloss over details or fail to mention the use of external plugins they, in most cases, do not source. Plus, before it gets mentioned, if there are any comments to be made that I’m “not searching in the right places”, I must reiterate, that I wouldn’t be posting here if I hadn’t exhausted searching in other areas.
All I am asking is, at the very least, to be pointed in the right direction for a solution to the function I am trying to create, if I am to have people waxing on about learning individual functions. That side, I am aware of the spelling methods in the script and I will of course be going over it again to check for capitalisation and proper spelling.
You state “You will be going over it again”. But if your code has update in it and not Update, it doesn’t behave the way you might think it will. Which is why I said, fix that and see if you get the behavior you want.
Pointed in the right direction?
Best I can say is, click once and the drop down opens.
When a page is clicked to open, add it’s gameobject to a list.
When the tab is clicked again, loop through the list and turn off everything in the list.
Clear the list.
Once broken down, the path to get what you want is actually a little more simple. It becomes a matter of tracking what is opened and then finding a way to access what you track and handle it as you need to.