Unity Change Scene with button - c#

I've been having problems with my code for unity. I'm using C Sharp and Unity 5.0.2f Personal Edition. Here's my code:
using UnityEngine;
using System.Collections;
public class ButtonEvent : MonoBehaviour {
public void LoadScene(int SceneToChangeTo){
Application.LoadLevel (SceneToChangeTo);
}
}
This should change the to an integer scene but when I go to the button.onclick() in the inspector and add the script nothing comes up about changing scene (Note: The script is under "_Manager" (An empty GameObject))

Add a listener for your button and make sure you put a value in numberOfLevel variable and assign your button to MyButton in the inspector or you will get a null reference exception
[SerializeField] private Button MyButton = null; // assign in the editor
public int numberoflevel;
void Start() { MyButton.onClick.AddListener(() => { changeScene(numberoflevel);});
}
public void LoadScene(int SceneToChangeTo){
Application.LoadLevel (SceneToChangeTo);
}

use this code:
using UnityEngine.SceneManagement;
///***///
public void LoadGameLevel(int SceneToChangeTo)
{ SceneManager.LoadScene(SceneToChangeTo);
}
//or
public void LoadGameLevel(string SceneName)
{ SceneManager.LoadScene(SceneName);
}
note: Worked in OnMouseDown() and Unity Ui and other

Related

Change function within unity inspector

I have a gameObject that needs to call lots of other functions. I was hoping to do something like this
Where I can select the gameobject and script for it to run. I can't figure out how to do this.
I'm calling a function on button click for a gui.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class AvatarSystem : MonoBehaviour
{
public Button general;
public Button face;
public Button hair;
public Button eyebrows;
public Button clothing;
// Start is called before the first frame update
void Start()
{
var root = GetComponent<UIDocument>().rootVisualElement;
general = root.Q<Button>("general");
face = root.Q<Button>("face");
hair = root.Q<Button>("hair");
eyebrows = root.Q<Button>("eyebrows");
clothing = root.Q<Button>("clothing");
general.clicked += generalClicked;
face.clicked += faceClicked;
hair.clicked += hairClicked;
eyebrows.clicked += eyebrowsClicked;
clothing.clicked += clothingClicked;
}
void generalClicked()
{
}
void faceClicked()
{
}
void hairClicked()
{
}
void eyebrowsClicked()
{
}
void clothingClicked()
{
}
}
The 5 bottom functions should do something like the top image. Thanks
You can this script which creates buttons in the inspector.
This should be outside of your AvatarSystem class.
using Unity.Editor;
[CustomEditor(typeof(AvatarSystem))]
public class AvatarSystemGUI : Editor
{
public override void OnInspectorGUI()
{
AvatarSystem t = AvatarSystem(target);
if (GUILayout.Button("Test"))
{
t.ExampleMethod();
}
}
}
Let me know of any problems! :)
You want to use a Unity Event
You can use UnityEvent<T> to pass a dynamic type in your function.
Then you can use Unity IPointerClickHandler to capture a click on the object.
You can use AddListener of UnityEvent;
you can write the code like this;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class AvatarSystem : MonoBehaviour
{
public Button general;
public Button face;
public Button hair;
public Button eyebrows;
public Button clothing;
// Start is called before the first frame update
void Start()
{
var root = GetComponent<UIDocument>().rootVisualElement;
general = root.Q<Button>("general");
face = root.Q<Button>("face");
hair = root.Q<Button>("hair");
eyebrows = root.Q<Button>("eyebrows");
clothing = root.Q<Button>("clothing");
general.onClick.AddListener(() => {
this.generalClicked();
this.faceClicked();
this.hairClicked();
this.eyebrowsClicked();
this.clothingClicked();
}) ;
}
void generalClicked()
{
}
void faceClicked()
{
}
void hairClicked()
{
}
void eyebrowsClicked()
{
}
void clothingClicked()
{
}
}

Unity back button

I am creating a unity app for android and want to implement back button functionality.I am using Unity 2019.1.11f1
I added an empty game object and attached a script to it which I have attached below.
public class BackButton : MonoBehaviour
{
private void Update()
{
if(Application.platform == RuntimePlatform.Android)
{
if(Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit();
}
}
}
}
I am expecting the app to quit but nothing happens.I also tried using GetKey and GetKeyUp instead of GetKeyDown.

Issues with fullscreen in unity

I'm making a simple menu in Unity 2D. I created a toggle that actives the full screen option. At this point, everything is alright.
Then, I wanted to do the same, but pressing the F11 key. Here starts the problem. When I press F11, the screen becomes in fullsize, but the toggle doesn't activates and vice versa.
So, I wanted to create a C# script that checks if Screen.fullscreen is true, it activates the toggle, and if it's false, it deactivates it.
I thonk that this will solve the problem, but no. When I try to press F11 or click on the toggle, the entire window goes crazy. I don't know how to explain it, but the window will start to shake on its own.
I would appreciate if somebody helps me, thank you!
The code of the toggle is this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ToggleScreen : MonoBehaviour
{
public void togglescreen()
{
if(Screen.fullScreen == true)
{
Screen.fullScreen = false;
}
else
{
Screen.fullScreen = true;
}
}
}
The code of the F11 key is this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FullScreenMode : MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown(KeyCode.F11))
{
if (Screen.fullScreen == true)
{
Screen.fullScreen = false;
}
else
{
Screen.fullScreen = true;
}
}
}
}
And the code of the activate and deactivate is this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ChangeValue : MonoBehaviour
{
private Toggle onOffSwitch;
private void Awake()
{
onOffSwitch = GetComponent<Toggle>();
}
private void Update()
{
//onOffSwitch.isOn = Screen.fullScreen;
if(Screen.fullScreen == true)
{
onOffSwitch.isOn = true;
}
else
{
onOffSwitch.isOn = false;
}
}
}
You are repeating the fullScreen logic in the ToggleScreen script and the FullScreenMode script. I would combine these (and simplify them) into a ScreenManager class:
using UnityEngine;
public class ScreenManager : MonoBehaviour
{
Toggle onOffSwitch;
void Start()
{
// Don't forget to attach the Toggle component to this game object
onOffSwitch = GetComponent<Toggle>();
// Subscribe to onOffSwitch's onValueChanged event handler
onOffSwitch.onValueChanged.AddListener(delegate
{
// delegate method to call when onOffSwitch.onValueChanged is raised.
ToggleValueChanged(onOffSwitch);
});
}
public static void ToggleFullScreen(bool updateToggleUI)
{
var toggledValue = !Screen.fullScreen;
Screen.fullScreen = toggledValue;
onOffSwitch.isOn = updateToggleUI ? toggledValue : onOffSwitch.isOn;
}
void ToggleValueChanged(Toggle change)
{
// The toggle was already changed via the UI, don't flip it back
ToggleFullScreen(false);
}
}
Then, simply call ScreenManager.ToggleFullScreen() in the FullScreenMode script's Update() method:
using UnityEngine;
public class FullScreenMode : MonoBehaviour
{
// Don't forget to drag the ScreenManager instance to this reference in the inspector
[Serializable]
ScreenManager _screenManager;
void Update()
{
if (Input.GetKeyDown(KeyCode.F11))
{
// Change the Toggle UI since this was triggered with F11
_screenManager.ToggleFullScreen(true);
}
}
}
public class Toggler
{
bool state = false;
public void ToggleState()
{
state = !state; // This sets a Boolean value to the opposite of itself
if (state)
{
// State was just toggled on
}else{
// State was just toggled off
}
}
}

Is there any way to switch to scene mode from game mode when pressing the play button?

So basically my question: Is there any way to switch from scene mode to gamemode script wise?
Image of what I want to do but from script
I am basically asking because I want to take a screenshot of the assets I load into the scene.
My code:
using UnityEngine;
using UnityEngine.SceneManagement;
public class NewBehaviourScript : MonoBehaviour {
// Use this for initialization
void Start () {
Debug.Log("HELLO");
var rss = AssetBundle.LoadFromFile(#"//MyFileLocation");
Debug.Log(SceneManager.GetActiveScene().name);
foreach (var asset in rss.LoadAllAssets<GameObject>())//<Texture2D>())
{
GameObject obj = Instantiate(asset, transform);
}
Debug.Log(transform.position);
Application.CaptureScreenshot(#"//MyScreenshotlocation");
Debug.Log("captured");
}
// Update is called once per frame
void Update () {
}
}
Any help is appreciated! Thank you!
You can switch to any Window with the EditorWindow.FocusWindowIfItsOpen function. To switch to Scene view, pass SceneView to it. You can use EditorApplication.playModeStateChanged to determine when you enter play mode.
This is an Editor plugin and must be placed in a folder named "Editor". Create a script called SceneSwitcher and copy everything below inside it. It should automatically switch to Scene View when you click the play button.
using UnityEditor;
[InitializeOnLoadAttribute]
public static class SceneSwitcher
{
static SceneSwitcher()
{
EditorApplication.playModeStateChanged += LogPlayModeState;
}
private static void LogPlayModeState(PlayModeStateChange state)
{
if (state == PlayModeStateChange.EnteredPlayMode)
SwitchToSceneView();
}
static void SwitchToSceneView()
{
EditorWindow.FocusWindowIfItsOpen<SceneView>();
/////OR
//SceneView sceneView = EditorWindow.GetWindow<SceneView>(); ;
//Type type = sceneView.GetType();
//EditorWindow.FocusWindowIfItsOpen(type);
}
}

How to code an UI button(with Exit funtion) for an Android device in Unity 3D(C#)?

I am a beginner of Unity 3D. I am writing an android app, and my start menu has an exit button. My idea is just very simple: after users press the exit button, the application will close.
I know Application.Quit() for quiting an app. But I just dont know how to detect a touch from user on a button and return the application.quit() method. Should I use sth like button.onclick or input.getTouch?
This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ExitButton : MonoBehaviour {
public Button exitButton;
void Start()
{
Button btn = exitButton.GetComponent<Button>();
btn.onClick.AddListener(TaskOnClick);
}
void TaskOnClick()
{
Application.Quit ();
Debug.Log("You touched this button.");
}
}
After I tried to play my app, I get this error:
NullReferenceException: Object reference not set to an instance of an object
ExitButton.Start()
And it seems that I cant get signal from user touching the button.
Can someone give me a help?
I have linked my button in inspector, but i am not able to find my function name:TaskOnClick()
Inspector
Put the following code into a script
public void QuitGame()
{
Application.Quit();
}
attach the script to a game object then assign the button to a method using the OnClick window at the bottom of the button component in the inspector.
Attach the game object to the OnClick and choose the QuitGame function.
Note. This will only work in the build of the game and not in the editor.
You are mixing up between two methods of using a button.
If you are following #Jack Foulkes's method then you don't need to assign the button externally. That means you don't need the following code:
public Button exitButton;
void Start()
{
Button btn = exitButton.GetComponent<Button>();
btn.onClick.AddListener(TaskOnClick);
}
If you use the above code, then you don't need to add OnClick event listener in inspector, as you are already adding it inside your code. Only your code will work. You are getting the null reference, because you haven't assigned the exitButton from inspector actually.
It should look like this in the inspector:
inspector
simple you have problem with access modifiers to fix this just type
public void TaskOnClick()
{
Application.Quit ();
}
check this video it's will help you create a professional gui.
How to make a professional GUI using unity
Create a script with the following contents and then attach it to the corresponding button click
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ExitButton : MonoBehaviour {
public void ExitGame()
{
print("Exiting...");
Application.Quit();
}
}
Hello,
All you need to do is to just make this function Public
like this:
public void TaskOnClick()
{
Application.Quit();
}
You Can Also make an if statement to make it much better :)
FOR EXAMPLE:
public void TaskOnClick()
{
if (Input.GetKeyDown(KeyCode.Escape) // You Can also change the key
{
Application.Quit();
}
}
And If you want to make it less effort MAKE a Function
TRY THIS:
public void ExitFunction()
{
if (Input.GetKeyDown(KeyCode.Escape) // Or Any Other Key
{
Application.Quit();
Debug.Log("exitgame");
}
}
Public void TaskOnClick()
{
ExitFunction();
}
And Also Here is the code if you want to make the same function but on the left Mouse Button Click
public void ExitFunction()
{
if (Input.GetMouseButtonDown(0))
{
Application.Quit();
Debug.Log("exitgame");
}
}
public void TaskOnClick()
{
ExitFunction();
}
If you want it with the Right Mouse Button
Here is the Answer:
public void ExitFunction()
{
if (Input.GetMouseButtonDown(1))
{
Application.Quit();
Debug.Log("quitgame");
}
}
public void TaskOnClick()
{
ExitFunction();
}
If you want it but with the middle buttonHere is the Answer:
public void ExitFunction()
{
if (Input.GetMouseButtonDown(2))
{
Application.Quit();
Debug.Log("quitgame");
}
}
public void TaskOnClick()
{
ExitFunction();
}
Full Best Code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ExitButton : MonoBehaviour
{
public void ExitFunction()
{
if (Input.GetMouseButtonDown(0))
{
Application.Quit();
Debug.Log("quitgame");
}
}
public void TaskOnClick()
{
ExitFunction();
}
}
Lesson Summary:
-- Sometimes Use public when we need it like when we use GameObjects and Transforms for example.
-- if () statement is used to make the I.D.E understands the very specific objects like Do an action when .... is pressed.
Thank you.
Hope This Helped
Eyad ,
A Professional Game Developer & Front End Web Developer
Do you link your button in inspector?
EDIT.
I think this should be changed also:
void TaskOnClick()
{
Debug.Log("You touched this button.");
Application.Quit ();
}

Categories

Resources