• 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 Scuba Kay · Feb 11, 2016 at 03:56 PM · scripting problemgameobjectevents

Is there a way to run a script on adding a script to an object?

I want to execute a script when I add a certain script to a gameObject. This is what I would like in code:

 class MyScript : Monobehaviour 
 {
     void onScriptAttached()
    {
         // Call some other script
         // In my case to generate some code
     }
 }

Is there any way to do this?

[edit]

Appearently I have not been clear enough. I don't want to add this script when playing. I want to drag and drop the script on top of a gameobject in the editor. Then when the script is added I want to run some code. I tried something using a custom editor that seems to be able to do this, but I don't think this is the way to go.

This would be the script to execute:

 public class CodeGenerator : MonoBehaviour
 {

     public void SaySomething()
     {
         Debug.Log("Something!");
     }
 }

And this is the code that makes it happen (I not taking it into account that I only want to execute this code once yet, because I don't think this is the proper solution):

 [CustomEditor(typeof(CodeGenerator))]
 public class CodeGeneratorInspector : Editor
 {
     CodeGenerator generator;

     // Use this for initialization
     void OnEnable () {
         generator = (CodeGenerator)target;
         SceneView.onSceneGUIDelegate += GeneratorUpdate;
     }

     void GeneratorUpdate(SceneView sceneview)
     {
         generator.SaySomething();
     }
 }

It just feels wrong to abuse the custom editors in this way, kinda hacky. What's the proper way of achieving this?

Comment
Add comment · Show 2
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 Cherno · Feb 11, 2016 at 03:59 PM 0
Share

You mean, sooner than the end of the current frame?

avatar image Scuba Kay · Feb 12, 2016 at 01:08 PM 0
Share

I meant inside the editor. So when you drag and drop your script on top of a game object the code executes.

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by meat5000 · Feb 11, 2016 at 03:59 PM

Simply place the calls to the other script inside the Start() of the script you add. Itll run automatically then, when the script is added.

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
avatar image
1

Answer by alankemp · Feb 12, 2016 at 01:34 PM

You can use the Reset function which only gets called in the editor when a script is first attached to an object.

http://docs.unity3d.com/ScriptReference/MonoBehaviour.Reset.html

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
avatar image
0

Answer by felixfors · Feb 11, 2016 at 08:08 PM

I havent tried the code but I think that it would look something like this.

 gameObject.AddComponent<scriptTest>();
         if(gameObject.GetComponent<scriptTest> !=null)
         {
             Debug.Log("We do have the script");
         }
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

46 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

Related Questions

Calculating Scrolling GameObject x position scrolling pass another GameObject x postion (2D Game) 1 Answer

setActive(false) does not fire onDisable? 1 Answer

How can I add the OnTriggerEnter function to all game objects that I instantiate? 1 Answer

Why are these object passing through each other? 1 Answer

How can I get the Transform from an Object (target of UnityEvent)? 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