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

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

Please help my head is burning from this problem : i have multiple gameobject , same script 1 Answer

Script on multiple objects not working properly! 1 Answer

Adding script during runtime 2 Answers

How to detect if two objects become a square ? 1 Answer

Object instances in code lifetime in comparison with gameobject 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