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 /
avatar image
0
Question by Khena_B · Mar 26, 2018 at 06:01 PM · editor-scriptingeditorwindowmonobehaviouraccesseditorscript

Acces monobehaviour script from editor script

Hey,

A bit new to editor scripts, i'm using a EditorWindow script made by the community to replace gameobjects in the scene with a prefab, but i'm trying to modify the script so that i can get and set some values of some of the scripts on these gameobjects. The first thing i noticed is that there is no way to access these monobehaviour scripts from the EditorWindow script, is there a way around that?

Comment
Add comment · Show 10
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 RobAnthem · Mar 26, 2018 at 06:07 PM 0
Share

Think of editor scripts as UnityEngine + UnityEditor. So basically you have full access to EVERYTHING PLUS THE EDITOR. To access a script from one of your gameobjects, you simply need to do something like this.

 $$anonymous$$yScriptType myScript;
 if (myScript = go.GetComponent<$$anonymous$$yScriptType>())
 {
     myScript.someValue = someNewValue;
 }

There's really hundreds of ways to gain a reference to an existing or new script, you just use the same references you would normally. Even Resources.Load() works for editor scripts. You can easily choose between editor code and runtime code to make whatever you want to happen. Even the EditorGUILayout and EditorGUI is completely interchangeable with GUILayout and GUI classes.

Point is, don't be afraid to use NON-editor code in your editor script.

avatar image Khena_B RobAnthem · Mar 26, 2018 at 06:14 PM 0
Share

Let's say the script i'm trying to access is called $$anonymous$$yScript.cs, in the EditorWindow script when i type $$anonymous$$yScript it doesn't find it, it doesn't seem like i have access to any of the monobehaviour scripts in my project, did i understand correctly?

avatar image RobAnthem Khena_B · Mar 26, 2018 at 06:17 PM 1
Share

If your script is within a namespace, you need to include that namespace in your editor class in a using statement. Otherwise the IDE won't recognize it. As well, if you are trying to access monobehaviours, be sure to include the using UnityEngine; namespace. By default if you create a script in Editor it will assign a namespace based on the folder, like namespace Assets.Scripts which is easily changeable, but you won't be able to reference classes inside a namespace without using the namespace.

If you're using Visual Studios, it will show you the hierarchy on the right hand side, there is one tricky thing to be aware of. If your scripts are in the Plugins Hierarchy, then they themselves will not have access to NON-plugins, but all NON-plugins can access the Plugins. $$anonymous$$oving a script from Plugins, Editor, or the main project, you can just grab it from the hierarchy and drag and drop it into the hierarchy you want it in. $$anonymous$$eep in $$anonymous$$d, this will not change the namespace, and occasionally has the effect of duplicating a class file.

Show more comments

2 Replies

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

Answer by Martin_Gonzalez · Mar 26, 2018 at 07:16 PM

Update to Answer

Scripts under Plugins folder are in Assembly-CSharp-Editor-firstpass.

Change the script to Assets/Editor/

Assembly-CSharp-Editor-firstpass and Assembly-CSharp-firstpass are compiled first, so they cannot know anything about other Assemblies classes

Comment
Add comment · Show 11 · 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 Khena_B · Mar 26, 2018 at 07:20 PM 0
Share

$$anonymous$$yScript can't be found, in $$anonymous$$onoDevelop it's just red, it's as if the scripts in an editor folder can't see any other scripts outside the folder, so i'm trying to figure out a way around that

avatar image Martin_Gonzalez Khena_B · Mar 26, 2018 at 07:21 PM 0
Share

Can you show me an image of your project hierarchy?

avatar image Khena_B Martin_Gonzalez · Mar 26, 2018 at 07:24 PM 0
Share

It's pretty standard, Assets/Scripts for my regular scripts, Assets/Plugins/Editor for my editor scripts

Show more comments
avatar image
0

Answer by NoMoneys · Mar 26, 2018 at 06:10 PM

For accesing a class on a gameobject, you should have a reference to it in some variable. Or atleast have a reference to the gameobject you're trying to get the class from.

Example:

Classname ourClass = (variable that holds gameobject).GetComponent();

then use the dot(.) operator to acces public fields and properties within that class.

Comment
Add comment · Show 7 · 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 Khena_B · Mar 26, 2018 at 06:17 PM 0
Share

I have a reference to the gameobject but i can't access the class, gameobject.GetComponent() doesn't work, i just can't seem to be able to access any monobehaviour scripts from an EditorWindow script

avatar image NoMoneys Khena_B · Mar 26, 2018 at 06:25 PM 0
Share

Does it give you an error of some sort?

avatar image Khena_B NoMoneys · Mar 26, 2018 at 06:27 PM 0
Share

There is no error, i just can't access any of my classes from an EditorWindow script, when i type the name of the classes it just doesn't exist

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

88 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

Related Questions

Saving a variable from an editor script into something like a gamemanager script? 0 Answers

Dynamic content in custom EditorWindow 1 Answer

Weird bug when dragging object onto EditorGUILayout.ObjectField 0 Answers

EditorGUILayout.LabelField not centered properly 1 Answer

Is there a way to get all editable fields of a component in a script? 2 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