Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
11
Question by graham · Apr 04, 2012 at 10:16 PM · gameobjecteditoreditor-scriptingcomponent

Execute Code When Component Added

I have a script that generates a simple mesh in a function called Rebuild(). Normally this function is called whenever Radius changes in the GUI. However, it doesn't get called when I first add the script/component to the a game object. What's the best way of doing this? I basically want to execute code on a component when it's added to a game object.

Thanks

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

6 Replies

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

Answer by fotaras · Jun 14, 2015 at 04:50 PM

The best way of doing that is using the reset

Comment
Add comment · Show 4 · 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 joonturbo · Jul 24, 2015 at 09:56 AM 1
Share

this is the correct answer

avatar image aeroson · Jan 02, 2016 at 01:34 AM 0
Share

for google: $$anonymous$$onoBehaviour void Reset() is basically Unity's on component added (onadded component) event (hook)

avatar image Justinas_Ma · Jul 27, 2017 at 05:24 AM 0
Share

Best solution! Unlike others, this one gets called if you attach a component on a disabled game object.

avatar image IgorAherne · Jul 02, 2018 at 09:29 PM 0
Share

Reset is not called when you ctrl+D an object, if that's something that you need (like assigning an internal ID). See my answer below if required.

avatar image
9

Answer by ConfucianFighter · May 03, 2016 at 06:24 PM

void Reset() is called on a monobehaviour in edit mode when it is first added.

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 AgentFire · Aug 12, 2016 at 12:51 PM 0
Share

The best answer here.

avatar image
-2

Answer by graham · Apr 13, 2012 at 08:32 AM

Thanks for the help mirswith, but I couldn't find what I needed directly. In the end, I used the ExecuteInEditMode attribute on my MonoBehaviour, and combined that with a flag that I check on Update. If the flag is set, I do what I need. Not the most optimal solution, but it seems to work and is simple.

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 Glurth · Feb 26, 2015 at 08:04 PM 0
Share

I created a class to derive from, based on your suggestion. The only thing I don't like about it is that any derived classes are ALSO subject to [ExecuteInEdit$$anonymous$$ode]. Usage: Override FirstPassFunction() with desired "OnCreation" behavior.

 using UnityEngine;
 
 [ExecuteInEdit$$anonymous$$ode]
 public class FirstPass$$anonymous$$onoBehaviour : $$anonymous$$onoBehaviour {
     
     [SerializeField][HideInInspector]
     private bool firstPassFlag=true;
 
     void Awake()
     {        
         if (firstPassFlag)
         {
             firstPassFlag = false;
             FirstPassFunction();
         }
     }
 
     protected virtual void FirstPassFunction()
     {
     }
 }
avatar image
-8

Answer by mirswith · Apr 04, 2012 at 10:42 PM

Take a look at MonoBehaviour and determine which event best fits your needs; Awake() maybe.

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 bloodthirst69 · Mar 17 at 01:25 PM

Use the "componentWasAdded" editor callback , here's an example i posted where it's being used

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
  • 1
  • 2
  • ›

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

12 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

Related Questions

How to add a component on a GameObject in Custom Inspector 1 Answer

How to undo removing components in an editor extension? 0 Answers

Add gameObject/transform to script component slot with editor 1 Answer

Change default sprite position when dragged into Hierarchy 1 Answer

How do you hide a GameObject's handles? 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