- Home /
The question is answered, right answer was accepted
array c# sending info from one script into another scripts array.
Hiya, so here's my problem.
I already know how to make an array, adjust its size, put different stuff in it and access them inside a single script. (making navigational locations for enemy's so they can patrol and such.)
now I want to make a closest enemy script in my selecting script. for that I need to make an array in my existing Selecting script and having other scripts (enemy scripts) send their info to my Selecting scripts array when they are within 100 meters or so and remove themselves from the array when they are further then 100 meters.
I figured out how to select them from my array but actually putting stuff in there from another script is still beyond me.
if you need like examples of what I got so far, just ask and i'll put it in.
hope someone can help me out with this problem.
Answer by pafla · Mar 01, 2014 at 12:43 PM
I would recommend, you use a List instead of an array, if you intend to add and/or remove elements frequently. A List would be much faster and easier to use in that case.
One way to do this would be to create public AddEnemy(EnemyInfo) and RemoveEnemy(EnemyInfo) methods in your class containing the array / List. The enemies would then use GetComponent()
to obtain the instance of that class and call those two functions, passing their data.
Some Links to help you:
http://www.dotnetperls.com/list http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html http://docs.unity3d.com/Documentation/Manual/GenericFunctions.html
sorry for the delay but I had to learn some extra shit about array's and lists to see if your answer was the answer I was looking for. and it is :) thnx for the help.