- Home /
How do you hide objects in Unity2D,
I want to hide some objects in a scene (more specifically when i press the options button on the main menu i want quit, play, options to disappear and back to appear) My code looks like this :
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Options : MonoBehaviour { public GameObject QuitB; public GameObject OptionsB; public GameObject PlayB; public GameObject BackB;
void OnMouseDown()
{
QuitB.SetActive(false);
OptionsB.SetActive(false);
PlayB.SetActive(false);
BackB.SetActive(true);
}
} But the buttons won't disappear and they still react to clicking. Is there any way to fix this? Or is there any way to hide an object? I've been searching for over 2 hours but nothing seems to work. I'm a Newbie i just started learning Unity and C#.
EDIT : What fixed my is that i was using prefabs instead of GameObjects and it turns out you can't "hide" prefabs with specific commands that can hide GameObjects THANK YOU ALL!
Are you sure that's getting called? What is the object holding your Options component?
What about the other question?
Are you sure that's getting called?
If unsure, add logging to the function
Answer by Vega4Life · Aug 01, 2019 at 04:03 PM
From what it looks like, and based on what was said. They are linking the prefabs of the buttons, not the actual ones in the scene. Probably because they are instantiating them in code, so they just need to get reference to the ones created - not the prefabs.
All of the Button objects have Box colliders which have "is trigger" enabled
If you create a cube, put a script on with:
private void On$$anonymous$$ouseDown()
{
Debug.LogError("TEST");
}
It will show up in the logs when you click the cube in the game scene (not the editor scene).
Yeah the Debug commands work with it including Debug.LogError, Debug,Log , Debug.LogWarning
Answer by I_Am_Err00r · Aug 01, 2019 at 03:42 PM
Ok, last guess here, where are you calling OnMouseDown()? Do you have something like this:
private void Update()
{
if(Input.GetMouseButtonDown(0))
{
OnMouseDown();
}
}
Because if you are just declaring void OnMouseDown, Unity doesn't know that means to call that if you are pressing the primary mouse button (or whatever you have it assigned to); you need to have unity check for when you press that and then call the function.
So I was wrong, turns out this is a standard unity event that can be called, but has some restrictions for it to work: https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.On$$anonymous$$ouseDown.html
Try adding an update that does what I said and then maybe change the name from On$$anonymous$$ouseDown() to something like Clicked(), here is an example:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Options : $$anonymous$$onoBehaviour
{
public GameObject QuitB;
public GameObject OptionsB;
public GameObject PlayB;
public GameObject BackB;
private void Update()
{
if(Input.Get$$anonymous$$ouseButtonDown(0))
{
Clicked();
}
}
void Clicked()
{
QuitB.SetActive(false);
OptionsB.SetActive(false);
PlayB.SetActive(false);
BackB.SetActive(true);
}
}
Let me know if that works.
The button's down in the "projects" windows disappear from their preview but they can still be seen on the "game" tab. Also if i end the testing of the game the buttons disappear like they were deactivated like i want them to. But for some reason i still see them in-game. Could this be because i add them to the scene with the *.SetActive(); command? video : https://youtu.be/YR-6BB5AWSU
Unity does detect this - its a built in method. It just needs to be on a UI element or something with a collider.