I have a game object button which I added a MonoBehaviour class using AddComponent. So, this MonoBehaviour class has one variable which I would like to assign a value to in the AddComponent phase. Is there any way I can do it?
CodePudding user response:
T AddComponent<T>()
returns the reference to the just added component instance.
So simply store it somewhere e.g. in a variable
var yourComponent = button.AddComponent<YourComponent>();
and then use it to e.g. assign a field
yourComponent.someField = someValue;