Launch a function of an editor script from another script
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
Answer by Harinezumi · Feb 14, 2018 at 04:14 PM
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.
Thanks, i tryied this and that work. But, how can i pause and play the game ? I don't want something who stop player movement, i would like that all the game stop. Thanks you in advance.
As a basic approach, Time.timeScale = 0; will stop updating the time of the application, for example animations, physics, and any code that uses Time.deltaTime or Time.fixedDeltaTime. When you want to unpause, you can just set Time.timeScale = 1;.
Ok. But if i stop the time, i will not be able to click on a dialog's button ?
If you use Unity UI you are still able to interact with it, and it will have the usual animations (e.g. button getting pressed).
Good ! $$anonymous$$y problem is resolved ! Thank you for your time and your help :)
Answer by LiamofElites · Feb 14, 2018 at 07:35 AM
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!
I have this following error : Assets/RN-Pack/Scripts/Dialog$$anonymous$$anager.cs(9,3): error CS0246: The type or namespace name QuitDialog' could not be found. Are you missing an assembly reference? I tryied to do using QuitDialog but don't work, and other error appear : Assets/RN-Pack/Scripts/Dialog$$anonymous$$anager.cs(4,7): error CS0246: The type or namespace name
QuitDialog' could not be found. Are you missing an assembly reference?
This is my noGameObjectScript:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using UnityEngine.Scene$$anonymous$$anagement;
public class QuitDialog{
public void Quitter(){
if(EditorUtility.DisplayDialog("Warning","Do you want to quit the game ?", "Yes", "No")){
Application.Quit();
}
}
public void Revenir(){
if(EditorUtility.DisplayDialog("Warning","Do you want to go back to the menu ?", "Yes", "No")){
Initiate.Fade("menu",Color.black,2.0f);
}
}
}
And this is my gameobject script :
using System.Collections; using System.Collections.Generic; using UnityEngine; using QuitDialog;
public class Dialog$$anonymous$$anager : $$anonymous$$onoBehaviour {
void FixedUpdate(){
QuitDialog noObj = new QuitDialog();
}
}
Thanks you very much if you find the solution !
This error is probably because in the gameobject script one of your using
directives is QuitDialog
. Since QuitDialog
is a class and not a namespace it's throwing that error. Heres what I mean
when it's appropriate to use Using
gameObjectScript.cs
namespace QuitDialog{
public class gameObjectScript : $$anonymous$$onoBehaviour{
void doSomething(){
//do somethign here
}
}
}
noGameObject.cs
using QuitDialog;
public class noGameObject{
QuitDialog.gameObjectScript.doSomething();
}
I know this may be confusing and to me this is the first time in a long time I'd had to refernce the Using
keyword. If I didn't explain it that well look at the Using Directive Reference that $$anonymous$$icrosoft has.
I will try this in the evening because today i'm working. But i am sure that work. Thank you very much, i will re-contact you this evening ;)
This response is completely unrelated to the actual problem.
This response to the response the is completely unrelated to the actual problem is completely unrelated to the actual problem.
Hmmm, very mature, especially downvoting my answer.
I was just stating that the answer you wrote does not address the actual issue that in builds you cannot use EditorUtility functions, and that is what causes the problem.
Your answer
![](https://koobas.hobune.stream/wayback/20220612152612im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The variables modified in the inspector are not kept on build 0 Answers
HideFlags.HideInHierarchy not updating hierarchy in Edit-mode 3 Answers
LensFlare component is missing Directional property in Script, does only appear in editor? 0 Answers
Custom inspector. Pick one bool from a list. 1 Answer
After Build Scripts no longer functioning in build or editor 1 Answer