Hi, i’m making a game in unity, and i want to add a function, when player click “escape”, the player got a dialog who ask player if he want to exit the game.
But i can’t attach an editor script to a gameobject.
In fact, the dialog work on unity, but when i want to convert my game in a windows executer, an error appear.
So now, i want to run an function from my editor script.
If you want to know my code, see this:
void FixedUpdate(){
if(Input.GetKeyDown(KeyCode.Escape) && SceneManager.GetActiveScene().name == "menu" && EditorUtility.DisplayDialog("Warning","Do you want to quit the game ?", "Yes", "No")){
Application.Quit();
}
else if(Input.GetKeyDown(KeyCode.Escape) && SceneManager.GetActiveScene().name == "game" && EditorUtility.DisplayDialog("Warning","Do you want to go back to the menu ?", "Yes", "No")){
Initiate.Fade("menu",Color.black,2.0f);
}
}
Thanks you in advance every body, i need your help !
(sorry for bad english i’m french!!)
Rémi
Hey! As it turns out if you want to execute a script without attaching to a gameobject then you have to do the following:
-Make sure the script doesn’t inherit from MonoBehaviour
-Use new MyScript();
to use the script in at least a method of something that is attached to a GameObject
For example:
noGameObject.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//don't inherit from MonoBehaviour, which means use noGameObject instead of
//noGameObject : MonoBehaviour
public class noGameObject{
void doSomething(){
//do something in here
}
}
GameObjecScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class noGameObject : MonoBehaviour{
void Start(){
//create an instance of noGameObject to use in a different script
noGameObject noObj = new noGameObject();
noObj.doSomething();
}
}
So in a way you Can run a script that isn’t on a GameObject but there has to be a script on a GameObject somewhere. My suggestion is place it on a GameObject that is static which means it never moves or gets messed with.
I hope this helps, if you have any questions/concerns feel free to contact me!
Hey!
You can’t use EditorUtility functions outside of the Unity Editor (hence the name: EditorUtility). They are designed to be used as debug functionality during development, but when you build, they are never included, and so you get the compilation error.
To resolve this problem you need to implement this dialog functionality yourself, probably with UI elements. For example create the UI (you will need a Canvas, at least a game object with Image and CanvasGroup component on it, a child with Text component and 2 with Button component). Then you should add to the base game object a script with the following in it:
private CanvasGroup ownCanvasGroup;
private void Awake () {
ownCanvasGroup = GetComponent<CanvasGroup>();
}
private void Update () {
if (Input.GetKeyUp(KeyCode.Escape)) { SetVisible(true); } // you might want to also pause the game here
}
private void SetVisible (bool value) {
if (ownCanvasGroup != null) {
ownCanvasGroup.alpha = value ? 1 : 0;
ownCanvasGroup.interactable = ownCanvasGroup.blocksRaycasts = value;
}
}
public void OnClickOk () {
// load the menu level using SceneManager.LoadLevelAsync() or exit the game using Application.Quit()
}
public OnClickCancel () {
SetVisible (false);
}
Finally, you need to link your OK button’s OnClick event to this scripts OnClickOk() function and the Cancel button’s OnClick event to the OnClickCancel() function.