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
3
Question by YoungDeveloper · Sep 07, 2013 at 05:24 PM · editoraccessotherfrom

Access other script from Editor script

Hi, is it possible to access other script from Editor script (no the one It's editing). As i understand i can't use getcomponent or other "usual" methods.

Thank you very much.

Comment
Add comment · Show 5
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 clunk47 · Sep 07, 2013 at 06:25 PM 1
Share

Have a look at ExecuteInEdit$$anonymous$$ode

avatar image YoungDeveloper clunk47 · Sep 07, 2013 at 06:31 PM 0
Share

I tried that already, but it still returns null reference.

avatar image Jamora · Sep 07, 2013 at 06:41 PM 1
Share

I can't test this right now, but doesn't ((TypeOfTarget)target).gameObject return the GameObject the target script is attached to?

avatar image YoungDeveloper · Sep 08, 2013 at 05:45 PM 1
Share

Here's an example of what i would like to achieve, is it somehow possible ?

 using UnityEditor;
 using UnityEngine;
 using System.Collections;
 
 [ExecuteInEdit$$anonymous$$ode]
 [CustomEditor(typeof(ScriptA))]
 
 public class ScriptAEditor : Editor {    
 
     ScriptB b;
     
     public override void OnInspectorGUI(){ 
         b.function(); //call of get something
     }
 } 
avatar image Sajidfarooq YoungDeveloper · Sep 08, 2013 at 05:52 PM 1
Share

Please use a "comment" to reply rather than creating an "answer".

2 Replies

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

Answer by Jamora · Sep 08, 2013 at 06:04 PM

I was right in my comment. You just need to cast is as MonoBehavior.

If your ScriptB is attached to the same GameObject, just do what I told in my comment. If it's in a different gameObject, you can do GameObject.Find or the other usual ways.

 ScriptB b;
     
 void OnEnable(){
     script = ((MonoBehaviour)target).gameObject.GetComponent<ScriptB>();
     b.YourFunction();
 }
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 YoungDeveloper · Sep 08, 2013 at 06:10 PM 0
Share

Unfortunately, It's not attached to the same GameObject.

avatar image clunk47 · Sep 08, 2013 at 06:22 PM 1
Share

Any editor script should be in the editor folder, not attached to a gameObject.

avatar image Jamora · Sep 08, 2013 at 06:37 PM 1
Share

How would you access your script normally then, from a non-editor class?

avatar image clunk47 · Sep 08, 2013 at 06:41 PM 1
Share

@YoungDeveloper, just clarifying, for other users.

avatar image YoungDeveloper · Sep 08, 2013 at 06:52 PM 4
Share

Yes, i found it. Posting for the record:

 GameObject go = GameObject.Find("somegameobjectname");
 ScriptB other = (ScriptB) go.GetComponent(typeof(ScriptB));
 other.DoSomething();

Found it from here: http://answers.unity3d.com/questions/7555/how-do-i-call-a-function-in-another-gameobjects-sc.html

And as all of you still spend time replying, everyone gets a thumb up (;

Show more comments
avatar image
0

Answer by onsboy · Aug 04, 2018 at 06:11 AM

I Would Like To Add That While **The Above Code IS Completely Correct.**Sometimes Unity Doesn't Update Properly and Still Throws Null Exception Error...

You can Also Write the Above Code Like This (As of Unity 2018.1.2f1):

 GameObject go = GameObject.Find("somegameobjectname");
 ScriptB other = go.GetComponent<ScriptB>()
 other.DoSomething();

{NOTE : Don't Do GameObject.Find in Update Loop.It Can Slow Down Game...Just for the beginners out there}

 In That Case Do A Couple OF things Which May Solve the issue :-
 
  1. Make Sure You Are In Play Mode.
  2. Clear The Cache.
  3. Restart Unity (If necessary PC)



Comment
Add comment · 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

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

20 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

Related Questions

Calling variables from other scripts 2 Answers

Accessing classes class from other script 2 Answers

Accessing function from another script won't work 1 Answer

Can't access variable from another script. 1 Answer

problems accessing variables and functions in another script 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