Where to initialize prefab's properties?

Hi,
i am developing a game where i dynamically create prefabs using Instantiate method:

var go = Instantiate(PlayerPrefab, position, rotation);
var player = go.GetComponent<Player>();
// then i set some initial values for player, for example:
player.color = red;

Note that i initialize some field values after Instantiate. In player script i need to read those values and initialize object (in this example it would be to find active material and set it’s color to player.color).

I could use OnStart method to do this, but it’s not reliable (sometimes it’s not called before first Update). Okay, so i can use Awake? Nope, it is called at the Instantiate, when player.color is not yet set.

So what method should i use to initialize my material color? Or maybe unity doesn’t have such method that is guaranteed to be called before first Update?

Hi there

Start is probably the correct place. I’m not sure what you mean by ‘not reliable’. Check out this diagram from the docs - it’s very handy! Start is guarunteed to be called once for every object, and will be called before FixedUpdate or Update.

OnEnable will (I believe) be called from within the call to Instantiate (note: worth checking - this is from memory), so your setting of player colour occur after OnEnable was called on the player component. Although I’ve not tried, Always will probably suffer the same issue.

-Chris