- Home /
Instantiate an enemy, best way to initialize behavior variables
I use Wave.js to Instantiate a few enemies at regular intervals. I need a good way to initialize the position of each as well as the path it will follow (multiple enemies follow the same path, but start staggered in position).
I cannot send parameters to Start(). I know of the SendMessage and GetComponent(ScriptName) possibilities.
I want to send the starting value (either x or y, depending on the math), the path it will follow (an integer) and some coefficients for my polynomials so the enemy can be self sufficient.
How do you suggest I initialize variables for these enemies after I Instantiate them?
Answer by FlowStateGames · Dec 18, 2012 at 03:25 AM
There are a couple of possibilities.
Assign all of the enemies as children of a manager object, instantiate in the parent Start().
SendMessage also works, as does GetComponent. Here's a really good summary of GetComponent
Arguably the 'best' (performance-wise) is to use delegates. Here is a great article about using delegates in a FSM. It may have info you don't need, but may actually point you in the direction of a great, efficient pattern.
I hope I haven't misunderstood the type of information you're looking for, and if so, I'll rework. But those articles should explain to you the ups and downs of the various methods you mentioned.
FlowState, that helped me use GetComponent in a better/easier way than I anticipated. I didn't even have to pass in parameters to a function, which I expected. I am glad I can have access to the parameters in the script, making it much easier to do what I want. Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220613085603im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
why wont this SendMessage work 1 Answer
.sendmessage not working C# 1 Answer
GetComponent don't work (i am desperate) 0 Answers
Instantiate And GetComponent 1 Answer