How do I create a button that links to another scene in my main menu (and by the way it is 2d). I tried the OnGUI code, it did link it to the scene, but it just linked it to the scene straight away after pressing the play button without clicking it.
You need a texture for the button, a nice 2D sprite will do. Just load through the editor. Then you add the code above to a script. Notice that you need an if, or else bad things happen :D (I could not resist the pun).
If you want to make it so that when you hit a specific button do it like this:
var texture: Texture;
function OnGUI()
{
if(GUI.Button(Rect(10,10,50,50), texture))
Application.LoadLevel("SceneName");
}
Or If you don't want to use the default GUI style provided by Unity you can do this:
function OnGUI()
{
var r = Rect(10,10,50,50);
GUI.DrawTexture(r, texture);
if(Event.current.type == EventType.MouseUp
&& r.Contains(Event.current.mousePosition))
Application.LoadLevel("SceneName");
}
Both versions of the code allow you to click the button and do something afterwards; in this case is switch scenes.
More on GUI stuff can be found here.
Related
I am creating a game project with a main menu and 3 other scenes (MyWorld, ImportWorld, Lab). I've made the buttons on the main menu clickable to go the 3 other scenes, and want to make a back button to return to the main menu from the 3 scenes. I managed to make the back button work on the Lab scene, but when I tried the same steps and scripts to the MyWorld and ImportWorld, it doesn't bring to the main menu although they are clickable and changes color when hovered and clicked.
Below is the current script I am using for the Lab scene which is working. However I had a problem when I wanted to change the name of the text, suddenly it didn't worked. But luckily I didn't save and just undo which makes the button workable again.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ReturnMainMenu : MonoBehaviour
{
public void returnMenu()
{
SceneManager.LoadScene("MainScene");
}
}
Below is the script I used for the Main Menu scene which opens up the 3 other scenes and works fine.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ButtonTrigger : MonoBehaviour
{
public void btnMyWorld() //function when clicked, open My World scene
{
SceneManager.LoadScene("MyWorld");
}
public void btnImportWorld() //function when clicked, open Import World scene
{
SceneManager.LoadScene("ImportWorld");
}
public void btnLab() //function when clicked, open Lab scene
{
SceneManager.LoadScene("Lab");
}
public void btnQuit()
{
Application.Quit();
}
public void btnReturnMainScene() //function when clicked, should return to Main Menu
{
SceneManager.LoadScene("MainScene");
}
//tried adding this function to the Back buttons but didn't work
}
I've tried various scripts (including entering scene index and scene name), changed the canvas position in hierarchy to be on top, have Event System and RayCast target but nothing seems to fix the problem. Does anyone know what might be the cause that makes the button clickable, changes color when hover but doesn't load the Main Menu scene? And as I mentioned I had problems with the workable back button at Lab scene which stops working when I changed my text name, so maybe does it have anything to do with the project settings/scripts?
Maybe you didn't put the scene in build setting.
As in my understanding from the information provided, you have established a working code, which you use in multiple areas and scenes.
Understanding from the issue you are presented:
A button has been established and is clickable Returning a colour change to prove it.
The text your tried to change is about the scene change within your script.
public class ReturnMainMenu : MonoBehaviour
{
public void returnMenu()
{
UnityEngine.SceneManagement.SceneManager.LoadScene("MainMenu");
//Has to be written exactly as the Scene you are aiming to load up.
//Change the steps to include SceneManagement.
}
}
Look up if it is written down correctly keeping Case sensitivity in mind.
Check if the scene is added to your Build settings Found here!
Another area to take a look at is, if you have attached the script to your button, where it gets told on what it is supposed to be doing, OnClick -> do this action.
See attached below!
(Make sure it is enabled for both: Runtime / Editor)
Make sure the button has "Interactable" as checked (Ticked on)
If you copied the button from another scene, within unity more often than not there will be issues with it that it won't work as intended, for that I recommend making a copy of the button you wish to use and make a prefab out of it. (Which can be used in every area that you desire and scenes you desire).
enter image description here
Unity has a component called Button as part of its UI system which you can use to subscribe on-click events to it through the inspector which is incredibly useful.
However, when projects get larger, I run into trouble in many situations:
events subscribed this way in the inspector are not rearrange-able which makes buttons that have lots of events difficult to manage
changing the contents of the scripts used for events can cause the button to not recognize a function that was used for an event which means you have to re-reference it
if anything happens to the GameObject or prefab that stores the Button component such as it getting corrupt then all your events that were serialized onto the button would be wiped and you would need to re-reference all of them
the above points make debugging very very difficult
What are some ways I can work around the problems I've listed above?
Inject event functions inside the code:
public Button playButton; // set button in inspector
public void Start()
{
playButton.onClick.AddListener(() =>
{
transform.position = point1;
// do something..
});
}
So I have a pause menu created in unity. There is a panel and on it, there are three buttons but they do not seem to be working, meaning I can not click on them. Also, interactable of every button is checked and event system is present in the hierarchy.
Hierarchy image
Following is the code for the resume button:
public void resumegame()
{
levemusic.UnPause();
pausemenu.SetActive(false);
Cursor.visible = false;
gameisPaused = false;
Time.timeScale = 1;
}
Either disable this component on the panel
Or move the panel object such that it is on top of the hierarchy in the object hierarchy
This is my first question here, i couldn't find an answer online for my problem.
Here it is:
I made a simple script to print some debug info about objects when i press a key. It works as expected unless i first move any of the objects in the scene editor while the game is running.
If i move any of the objects after i hit play it seems that Input.GetKeyDown is ignored after that. I am detecting the input inside the Update function of one of the objects.
public GameObject target;
void Update ()
{
if (Input.GetKeyDown(KeyCode.P))
{
Debug.Log (transform.position);
Debug.Log (target.transform.position);
}
}
NOTE: this is not the only thing im trying to achieve with my script, but is the simplest case i could build with the same problem.
thanks in advance!
I think that's because when you move the object, the Unity GameView will lose its focus. So just ensure it has the focus again (klick into it), before hitting the key.
I want to create a PopUp for my game, my requirement is to open a popUp when user click a button.
And popUp contains a image for its background, a close button on upper-right corner and two buttons on the popUp (lets say YES & NO).
I make R&D but nothing found relevant.
Any help would be appriciated.
P.S. I don't want to use any third party plugIn Like NGUI, 2D ToolKit etc.
Unity till 4.5
You can build most components with GUITexture wiht the legacy UI system.
http://docs.unity3d.com/Manual/class-GuiTexture.html
Build your background and buttons from textures of the scheme below. For the buttons you also use GUIText and make the clickable/touchable.
For more info see the scripting guide.
http://docs.unity3d.com/Manual/GUIScriptingGuide.html
Unity 4.6 or newer
The GUI system of Unity easily allows you to do that. You will need a Canvas and a Sprite for the background. And two buttons as children for YES and NO.
See the introduction first
http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/the-new-ui
The manual shows all components
http://docs.unity3d.com/Manual/UISystem.html
GUI Reference
http://docs.unity3d.com/ScriptReference/GUI.html
Try creating something like this for the pop-up button(JavaScript):
var popupactive : boolean = false;
var (Name of the GUI window) : GUITexture;
var (Picture1) : GUITexture;
var (Picture2) : GUITexture;
var (Picture3) : GUITexture;
(Name off the GUI window).enabled = false;
(Picture1).enabled = false;
(Picture2).enabled = false;
(Picture3).enabled = false;
function OnMouseUp(){
if(popupactive==false){
popupactive = true;
}
else{
popupactive = false;
(Name of the GUI window).enabled = false;
}
}
Then try adding a function that closes the GUI for the exit button and "no" button to close the opened GUIs, Also remember to import the separate scripts assigned to the separate buttons inside the code! Remember also to assign them in the Unity editor. Hope it helps!