Can't get Panel to Close

I have a whiteboard I made using the freedraw asset, and it writes just fine, but I don’t know why I can’t get it to close. I tried using buttons, but it wouldn’t press the button, there was always something in the way, so I made my own button with a raycast, and a sprite with a circle collider. It seemed to be hitting the white board instead of the button, even though it was on top on both sorting layer, and z axis, so I designed my own button using raycasts.

Here’s the script,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpecialButton : MonoBehaviour
{
    Camera cam;
    public GameObject whiteBoardIcon;
    public GameObject whiteBoard;
    public LayerMask whiteBoardMask;
    RaycastHit hit;
    Ray ray;

    private void Start()
    {
        cam = Camera.main;
    }
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            print("Nope");
            if (Physics.Raycast(ray,out hit, ~whiteBoardMask))
            {
                print("Almost");
                if (hit.collider.CompareTag("Close"))
                {
                    Close();
                    print("You did it!");
                }
            }
          
        }
    }

    void Close()
    {
        whiteBoardIcon.SetActive(true);
        whiteBoard.SetActive(false);
    }
}

At the moment, only the “Nope” is printing.I have a circle collider, i have the tag of the close button set, and I have the whiteboard on the whiteboard layer, which is set in the inspector to white board. I can’t for the life of me figure out why it isn’t working… Also, it is on the canvas, which shouldn’t matter, but here’s what my hierarchy looks like just for reference.

I’ve tried a million different alternatives, it seemed to work better when I had it declaring a Physics.Raycast, it was at least printing the second one, but I cannot get this to work…Would someone be able to tell me what I’m doing wrong?

Thanks in advance

You’re certainly setting yourself up for a lot more grief by trying to create your own button instead of just using the Button component. If it wasn’t working, let’s try to figure out why and fix it. It will be easier than writing your own button class.

1 Like

Well, I tried for like an hour haha. I cannot figure out why it’s not working. The buttons don’t even seem to go on top of the whiteboard, so I even tried just making a sprite on top of the white board, and putting a button component on it. No luck.

Well, I made a new button, and somehow got it working… I don’t know why, but it seems like removing the text component from the buttons is what made them unclickable?? lol, I created a new button, removed the image, replaced it with a sprite, moved it on top, and just blanked out the text field.

I can’t say I understand what caused me to struggle for three hours, but I fixed it… lol

I’ve been making apps since I’ve joined the reason why some buttons are unclickable is because of the order in the hierarchy. For example, on a canvas if you put a panel at the bottom as a child of the canvas it’d cover over the other buttons.

1 Like

It was so strange though, I moved the button everywhere… I think there’s something weird in this drawing script i got for free lol, cause when I actually had it hitting a collider, it kept printing the whiteboard’s name… Even when I shrunk the whiteboard to next to nothing lol

Collider? You are using UI right? Or is that you’re using 3d objects as a button? Rather explain what you’re trying to do so I can understand what you’re trying to do.

Ah, sorry, I already have it fixed. I mean as I was trying to use a raycast to do it in an alternate way it was printing the collider of the whiteboard. So I think it was blocking the buttons, but idk.