Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by RemiNatueur · Feb 14, 2018 at 07:15 AM · scripting problemeditor

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
-2
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RemiNatueur · Feb 14, 2018 at 06:48 PM 0
Share

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.

avatar image Harinezumi RemiNatueur · Feb 15, 2018 at 08:13 AM 0
Share

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;.

avatar image RemiNatueur · Feb 15, 2018 at 08:23 AM 0
Share

Ok. But if i stop the time, i will not be able to click on a dialog's button ?

avatar image Harinezumi RemiNatueur · Feb 15, 2018 at 10:00 AM 0
Share

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).

avatar image RemiNatueur · Feb 15, 2018 at 09:14 PM 1
Share

Good ! $$anonymous$$y problem is resolved ! Thank you for your time and your help :)

avatar image
0

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!

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RemiNatueur · Feb 14, 2018 at 08:02 AM 0
Share

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 !

avatar image LiamofElites RemiNatueur · Feb 14, 2018 at 08:53 AM 0
Share

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.

avatar image RemiNatueur · Feb 14, 2018 at 12:38 PM 0
Share

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 ;)

avatar image LiamofElites RemiNatueur · Feb 14, 2018 at 12:39 PM 0
Share

Ok! Have a good day at work!

avatar image Harinezumi · Feb 14, 2018 at 04:02 PM 0
Share

This response is completely unrelated to the actual problem.

avatar image LiamofElites Harinezumi · Feb 14, 2018 at 04:46 PM -1
Share

This response to the response the is completely unrelated to the actual problem is completely unrelated to the actual problem.

avatar image Harinezumi LiamofElites · Feb 14, 2018 at 05:00 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

207 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges