UI Problem

Hello,

I have some UI elements which includes a button that when pressed is supposed to disable the first child under the game object with the script on, but it deactivates the object with the script as well. I’ve literally tried everything that I found in a google search for example

transform.Find(“Child Name Here”).gameObject.SetActive(false);
transform.Find(“Child Name Here”).transform.gameObject.SetActive(false);
gameObject.transform.Find(“Child Name Here”).gameObject.SetActive(false);
transform.GetChild(0).gameObject.SetActive(false);

heck I even tried making a GameObject variable and setting that in the inspector, and still it disables the UI gameobject that the script is on.

What am I missing? I’ve been vigorously trying to figure out how to do this for the past couple of hours and I’m at my wits end lol. pls send help

First, I would HIGHLY recommend deleting ALL of those Find() calls. Instead, just make public fields and drag them in.

If you insist on Find() then it’s time to doublecheck you’re finding the right thing. See below.

Welcome to debugging! Here’s how to get started.

You must find a way to get the information you need in order to reason about what the problem is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

When in doubt, print it out!™

Note: the print() function is an alias for Debug.Log() provided by the MonoBehaviour class.

Haha, sorry I didn’t clarify, I don’t have a bunch of .FInd() calls in a row, those are just the ones I tried on a single line, and every single one, disables the UI Gameobject that the script is on, instead of the one in Find(" "). I’ll continue to try, I thought about just restructuring my UI, because how I have it setup is, Empty GameObject named
:::UserInterface:::
Canvas < - Has player HP/Stamina
Canvas < - Has and exp bar/text
Empty < - Has the script that supposed to do the thing, attached to this object is the script
Canvas (x) < - this screen is supposed to pop up when you level up and turn off after selecting a Card(stat boost)
Empty
Image
Empty <This one has all the image/buttons. And when I click one of these buttons it’s supposed to turn off Canvas (x)

Yes I’ve triple checked that the spelling is correct, it has the correct amount of spaces. It simply will not turn off Canvas (x) it turns off the Empty above Canvas (x).

Anyway I’m going to keep trying to figure it out.

Edit For clarity, I’m also using the ‘New’ Input System. Oh and trust and believe I llittered debugs everywhere, I really think it’s possibly because of the way I have my UI structured, I’ll post back when/if I figure it out.

Ok, So I can’t really say that I figured out the problem. I just went ahead and refactored my code and reworked my hierarchy and I think that was the best approach because I want to keep my project as organized as possible. I think thats probably why I was getting that issue.

What I did was made it so that I had to set the specific UI gameobjects in the inspector and the script is on a Gameobject outside of the UI therefore it can access code whether or not its active. Thanks @Kurt-Dekker for the hand!

1 Like