Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by tXxy · Jan 02, 2021 at 05:16 PM · editorreferenceeditorwindowmonobehaviour

How to reference an EditorWindow script in a MonoBehaviour script OR how to pass variables from a EditorWindow to a MonoBehaviour script?

Hello, I am right now trying to build an EditorWindow from which I can activate my boosters, it works very simple, if I click the button on the EditorWindow the bool gets set too true.
Now I want to pass the bool variable to my playerMovement script, a MonoBehaviour script normally I would just reference the script where I set the bool variable, but I can not attach the editor window script to the playerMovement script, after some time googling I saw that you can't reference an EditorWindow script inside a MonoBehaviour script.
So how I do pass a bool variable from the EditorWindow to the PlayerMovement script?

My EditorWindow looks like this:

 public bool speedBooster;
     
         [MenuItem("My Windows/BoosterWindow")]
         static void Init()
         {
             boosterWindow window = (boosterWindow)EditorWindow.GetWindow(typeof(boosterWindow));
             window.Show();
         }
     
         void OnGUI()
         {
             GUILayout.Label("Active the boosters from the buttons below");
     
             if (GUILayout.Button("SpeedBooster"))
             {
                 speedBooster = true;
             }
         }

And my playerMovement will use the variable like this:

 public class PlayerMovement : MonoBehaviour
 {
  public boosterWindow bw;
    
     private void Update()
     {
         if (bw.speedBooster)
         {
             boosting = true;
             speed -= 5;
         }
     }
 }

Can anybody suggest me an idea of how I can tell the playerMovement script that the bool is true so that the speed variable gets changed?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by trannel · Jan 02, 2021 at 07:51 PM

Dont reference Editor windows in monobehaviour its the wrong way around.

..

It looks like you do not want an Editor Window though. just an Editor.

an Editor class has a target property (the monobehaviour).

usage:

 [CustomEditor(typeof(PlayerMovement))]
 public class PlayerMovementEditor : Editor {
     public override void OnInspectorGUI()
     {
         base.OnInspectorGUI();
 
         if (GUILayout.Button("boost player"))
         {
             Debug.Log((PlayerMovement)target);
             ((PlayerMovement)target).speedBooster  = true;
         }
 
     }
 
 }

if you realy want/ have to to use an EditorWindow, you will have to find the target monobehaviour. using e.g https://docs.unity3d.com/ScriptReference/Transform.Find.html or https://docs.unity3d.com/ScriptReference/Selection-gameObjects.html or many other ways.

Comment
Add comment · Show 1 · 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 tXxy · Jan 02, 2021 at 09:00 PM 0
Share

Thank you very much, this way I could figure it out! I am a bit sad that it is not possible the way I wanted it to be, I thought having all boosters in a separate window would make it easy to test them out without needing the player to pick them up in the world. But Now I can just active them on the playerscript which is fine too :)

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

155 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

Related Questions

Create a custom fullscreen game view window 0 Answers

EditorGUI.PrefixLabel broken? 2 Answers

Draw Camera to Editor Window 1 Answer

I have an undocked editor window/tab. How can I (via C# editor code) simulate a click on this button? 0 Answers

Bug? Overlay Menu will not open using Space or any other key (2021.2.7) 0 Answers


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