- Home /
Question by
nizameddinkale · Jan 28, 2021 at 08:36 AM ·
c#unityeditorattribute
How can script will auto run on attaching a gameobject in editor?
I have a func with a contextMenu attribute. Every time i have to attach this to a game object afterward i have to click on GenerateSomething. (Lazyness whispering me) is there way to make this auto run attaching on a game object?
[ContextMenu("GenerateSomething")] //auto run once on attach?
private void GenerateSomething()
{
//spawn something or generate id idk
}
Comment
Answer by nizameddinkale · Jan 28, 2021 at 11:45 AM
i found some other solutions to this:
https://learn.unity.com/tutorial/editor-scripting/?tab=overviewCreating button instead of right click select method
I was thinking this what if i forgot to assign it. which other solution i found is warn yourself
#if UNITY_EDITOR
private void OnEnable() {
if (id == string.Empty)
{
Debug.LogError($"{ gameObject.name } gameObject id is not set!");
}
}
#endif
[ExecuteInEditMode]this solution below is not recommended but putting it here
//your monobehaviour class
this attribute runs your script's onawake enable etc. at editor while not in play mode
Your answer
![](https://koobas.hobune.stream/wayback/20220613024127im_/https://answers.unity.com/themes/thub/images/avi.jpg)