Setting values in instance before Start()?

Hey there folks. I’m pretty new so apologies for a probably silly question. Is it possible to pass an Instance some parameters before its Start() gets executed? Because this is what I’d like to do:

  • Create an instance of a script

  • Set some values

  • Execute its Start()

My current work-around is by adding a new methodPostStart()to the instance and calling it like this:

foo = gameObject.AddComponent();
foo.someInitialValue = “bar”;
foo.PostStart();

Ideally I’d love to have something like this:
foo = new Foo(“bar”);
but Unity tells me to use AddComponent instead of new…

Well sounds like you are trying to add the script which yes of course you will need the addComponent to add the script. After I don’t understand why you couldn’t do foo = new Foo(params); as this is a constructor and can be done. Just setup a constructor inside your class, and good to go? Best to look up constructors with Unity on how to accomplish this.

Use Awake()

It gets called before Start()

1 Like

Thanks for your suggestions. Alright, I guess it works when I create a sub-class with a proper constructor like I head before. My error was that it was not a sub-class but a wholly separate class and that’s why Unity wasn’t allowing it.

I grasp the basics of OOP and love to compartmentalize my code but I still have a lot to learn about best practices because I am over-complicating things for myself in my little test prototype :slight_smile: