So, i have buttons for my game that include : Main Menu button , Restart button and Play button. Everything works fine, but occasionally one of the button's text doesn't load , one of them doesn't work. I'm not sure how to fix this, because it happens randomly and not every time.
The code for the buttons :
GameObject[] pauseObjects;
void Start()
{
Time.timeScale = 1;
pauseObjects = GameObject.FindGameObjectsWithTag("ShowOnPause");
hidePaused();
}
public void showPaused()
{
foreach (GameObject g in pauseObjects)
{
g.SetActive(true);
}
}
public void hidePaused()
{
foreach (GameObject g in pauseObjects)
{
g.SetActive(false);
}
}
public void LoadLevel(string level)
{
Application.LoadLevel(level);
}
public void pauseControl()
{
if (Time.timeScale == 1)
{
Time.timeScale = 0;
showPaused();
}
else if (Time.timeScale == 0)
{
Time.timeScale = 1;
hidePaused();
}
}
The way your UI is setup is not right. If you have to loop through everything during pause, you are doing it wrong. I have answered a similar question before and will just copy and paste this here.
Use different Canvas to separate your UI and group them depending on when they are displayed. Then you can toggle the whole Canvas on/off. Your [post][1] before this post shows that you are using image as button and you have a different script attached to two images you are using as buttons. Use Buttons for buttons and image for non-clickable objects. Simple as that. So change those images to buttons.
You don't need the GameOver scene. A GameOverCanvas is fine.
The GameObjects in bold are the parent Objects(Canvas). The ones under that starts with '-' are the child GameObjects.
Step 1:
Create Canvas and name it MainMenuCanvas (First UI to display when game Loads).Create each child button and rename them as below(GameObject->UI->Button):
-playButton;
-settingsButton;
-exitGameButton;
Attach the MainMenuCanvas script to the MainMenuCanvas Object.
Step 2:
Create a Canvas and name it GameCanvas (Displayed during game).Create each child button and rename them as below(GameObject->UI->Button):
-pauseButton
-jumpButton
Attach the GameCanvas script to the GameCanvas Object.
Step 3:
Create a Canvas and name it PauseCanvas (Displayed when pause button is clicked).Create each child button and rename them as below(GameObject->UI->Button):
-resumeButton;
-backToMainMenuButton;
-settingsButton;
-exitGameButton;
Attach the PauseCanvas script to the PauseCanvas Object.
Step 4:
Create a Canvas and name it SettingsCanvas (Displayed when settings button is clicked).Create each child button and rename them as below(GameObject->UI->Button):
-backButton;
Attach the SettingsCanvas script to the SettingsCanvas Object.
Step 5:
Create a Canvas and name it GameOverCanvas (Displayed when Game is Over or player is killed).Create each child button and rename them as below(GameObject->UI->Button):
-playAgainButton;
-backToMainMenuButton;
-exitGameButton;
Attach the GameOverCanvas script to the GameOverCanvas Object.
Step 6:
In the Game scene, make sure that only GameCanvas is enabled. The rest of the canvas should be manually disabled.
Step 7:
In the Menu scene, make sure that only MainMenuCanvas is enabled. The rest of the canvas should be manually disabled.
Once you get this setup correctly, the UI code templates I provided should work. No more UI overlapping or text disappearing. You can easily add or remove features.
Your UI setup should look like the image below.
Related
New to coding here, setting up a UI in unity and having a visual issue where the back button appears until I hit options and back on the start up page, any reccomendations for a fix?
Image showing back button behind the quit button
Settings of the image on the right
You can use Gameobject.setactive(true/false) and try to manage the panel by making empty game object inside UI Canvas and name it as a panel so the whole button became a child and you can easily manage it
for example like this one
public Gameobject startPanel;
public Gameobject optionPanel;
public void Startpnl()
{
//deadActive optionPanel and Activate startPanel
startPanel.setActive(true);
optionPanel.setActive(false);
}
public void optionpnl()
{
//deadActive starPanel and Activate OptionPanel
startPanel.setActive(false);
optionPanel.setActive(true);
}
How would you access the Options/Settings Menu from an in-game Pause Menu?
I understand loading the different scene on the button click, but the scene I load (via loadscene) opens with my Main Menu UI. I want to load the Options/Settings UI when I load the Main Menu scene. I think just loading the Main Menu scene, then deactivating the Main Menu UI gameObject and activating the Options/Settings UI gameObject would be sufficient, but it only loads the Main Menu UI on scene change.
Here is my code for the button click (for loading the Options/Settings Menu UI):
public enum GameOverStates
{
Main,
Store,
};
public GameOverStates currentState;
public GameObject mainMenu;
public GameObject storeMenu;
void Update()
{
switch (currentState)
{
case GameOverStates.Store:
mainMenu.SetActive(false);
storeMenu.SetActive(true);
break;
}
}
// Switch to Store Menu
public void OnStore()
{
UnityEngine.Debug.Log("The fucking store");
//Change menu state
Loader.Load(Loader.Scene.MainMenu);
currentState = GameOverStates.Store;
////Play sound effect
SoundManager.PlaySound(SoundManager.Sound.ButtonClick);
}
Thank for you any help :)
you can use DontDestroyOnLoad to keep your main menu in any other scene. just call it on Start function and MainMenu object will be not destroyed by SceneManager. (Read here)
Use SceneManager.MergeScenes to append two different scene on loading the new one. so it will keep your main menu and add your other scene to it. (Read Here)
I am new to Unity and was wondering if it was possible to check if a button component in present in the scene, this is like a UI test.
I am trying to do this using the Unity UI Test Automation tool.
While zyonneo answer is correct, GameObject.Find can be slow if there is a lot of gameObjects.
What I would do instead is looking for the button component in the canvas children. For this you need a reference to the parent canvas of your button.
public GameObject MyCanvas;
void Start(){
if(MyCanvas.GetComponentInChildren<Button>() != null){
Debug.Log("Button found");
else {
Debug.Log("Button not found");
}
}
If you want more information about what is the most efficient way to find a gameObject, I recommend you to read this thread on Unity Formus.
Add the code where you want to check whether the button is present in Hierarchy.If you have added the button in the canvas you can find using the below code.Just type the exact name of the button.
By default when you add a button it will have the name "Button" so search with that parameter.Here I am adding a button by right clicking and then renaming it into "Btn01"
if(GameObject.Find("Btn01")!= null)
{
Debug.Log("Button is Present in Hierarchy");
}
else
{
Debug.Log("Button Not Present");
}
I have a fadepanel gameobject in my scene that has to sit on top of all the other gameobjects. However, when I click on an object int the editor window, since this gameobject is on top of all, is the one that is automatically selected in the hierarchy.
Is there a way to keep the gameobject on top of the canvas, as it is right now, but make it completely unselectable wIen i click on my scene?
You can use HideFlags on gameobject to make it unselectable:
public class HideFlagsSetter : MonoBehaviour
{
public Component target;
public HideFlags customHideFlags;
public enum Mode
{
GameObject,
Component
}
public Mode setOn = Mode.GameObject;
[ContextMenu("Set Flags")]
private void SetFlags()
{
if (setOn == Mode.GameObject)
{
target.gameObject.hideFlags = customHideFlags;
}
else if (setOn == Mode.Component)
{
target.hideFlags = customHideFlags;
}
}
}
Setting customHideFlags to HideInHierarchy would make it disappear from hierarchy so you won't be able to select it.
Edit: As the object will disappear, there is no way to bring it back if you can't access the script. So this script should be attached to a persistent object and target object should be set in the inspector.
You can use Context menu option by clicking on the gear icon in the top right corner and choose "Set Flags". its the last option in the menu.
Read more
Hope this helps :)
(ADDED BY PERSON WHO ASKED THE QUESTION)
I wanted a way for this to automagically happen on editor mode. So i did the same steps, but i changed the script a bit to work on Editor mode. I added [ExecuteInEditMode] on top of the class and also added an Awake() method that executes the same code as in SetFlags. Seems to work just fine.
I have three buttons, when you tap one button, a panel with a text appear.
The problem is when I attached the script, nothing happens.
The "click" is registered but the the panel never appears.
My script is attached to each of the button and is something like this:
public GameObject panel; //i use to put the panel in unity
bool selected = false;
void Start () {
panel.SetActive(false);
}
void OnSelect() {
selected = !selected;
panel.SetActive(true);
}
I probably have to do something else with the panel but I can't figure it out.
Do it like this:
(1) Add the canvas to your project
(2) BIG TIP - be sure to select Scale with screen size.
That's the only one you ever use. Unity accidentally set the wrong default there, they have not fixed it yet.
(3) In your Canvas, add a BUTTON Make it say perhaps "Test"
(3) In your Canvas, add another BUTTON Make it say perhaps "Another Test"
(4) Make a script something like this...
public class MainScreen:MonoBehaviour
{
public void UserClickedTest()
{
Debug.Log("test..");
}
public void UserClickedAnotherTest()
{
Debug.Log("another test..");
}
}
(5) put ONE copy of that script on ANY object you like. You can put it on your camera, on the canvas, or anywhere else that makes sense
For now let's say you put it on your CAMERA object, for example.
(6) Click on the button "Test" .....
And do this ...
click the PLUS button under OnClick
you see the slot that says "_main" in this example. DRAG your CAMERA item from HEIRARCHY, to that slot
Using the drop down menu:
select the "UserClickedTest()" function ...
good eh?
Now for the other button, do the same but select the "UserClickedAnotherTest()" function.
You're done! Run and test!
You can't use the OnSelect system unless you use ISelectHandler and more stuff: it is difficult for beginners. I strongly recommend the OP masters the simpler technique I explain here. Enjoy!
Maybe you attached the script to the panel. If so, your scripts can not be executed as long as your GameObject is SetActive(false).
I hope I was able to help you.