- Home /
Add Transform to array
How do I add a transform which is instantiated?
My problem is, I have some enemies. I they all have a script with a transform array in it. When I spawn in, I want to add my own transform to the enemies enemy array!
Code:
ENEMY SCRIPT:
public Transform[] Enemies;
SPAWN IN SCRIPT:
public GameObject Player; //Assign in unity
void Start(){
Instantiate(Player);
//Add transform to Enemies[] which is in ENEMY SCRIPT
}
I mean, add a transform from an object which is instantiated. :)
This question, on its own, is nowhere near detailed enough to get a meaningful answer. With the clarification below (as a comment to james_170482's answer) it becomes slightly more viable. Could you be more specific as to how you're spawning the enemies and populating their original array? Through the Editor?
It would also be appreciated if you could edit your question when making clarifications so that people don't have to scroll through all the comments in all the answers to figure out its current state.
Answer by Kiwasi · Nov 06, 2014 at 11:42 PM
Use a generic list instead, its better for this application
public class Player : MonoBehaviour {
public GameObject Player;
public EnemyScript enemyScript;
void Start(){
GameObject clone = (GameObject)Instantiate(Player);
enmeyScript.enemyList.Add(clone.Transform)
}
}
public class EnemyScript : MonoBehaviour {
public List<Transform> enemyList = new List<Transform();
}
Lists are much easier to use. If you are planning on adding,removing, and changing the number of objects contained at runtime, use a list
You can do an array if you like. The code is pretty similar. The problem with an array is arrays are of fixed size. You will have to specify the size in advance, and figure out which index you want to add the item to. List takes care of all of that for you.
Why are you so set on using an array? There is nothing an array can do that a list can't. Under the hood a list is an array.
Answer by james_170482 · Nov 04, 2014 at 11:07 PM
Try this Untested code;
public Tranform[] myTransforms;
public int NumberOfTransformsYouNeedToStore = 10;
public Gameobject myGameobject;
void Awake()
{
mytransforms = new Transform[ NumberOfTransformsYouNeedToStore ];
}
void Start()
{
Gameobject go = Instantite( myGameobject ), new Vector3( 0, 0, 0 ), Quaternion.identity) as Gameobject;
myTransforms[ arrayPositionNumberBetween " 0 and NumberOfTransformsYouNeedToStore - 1"];
}
$$anonymous$$y problem is, I have some enemies. I they all have a script with a transform array in it. When I spawn in, I wan't to add my own transform to the enemies enemy array!
fx:
ENE$$anonymous$$Y SCRIPT:
public Transform[] Enemies;
SPAWN IN SCRIPT:
public GameObject Player; //Assign in unity
void Start(){
Instantiate(Player);
//Add transform to Enemies[] which is in ENE$$anonymous$$Y SCRIPT
}
Do you need each enemy to hold an array of all other enemies, wouldn't an empty gameobject with a script holding one public array be better or am I misunderstanding again lol
All my enemies have an script which tells them to go to a target. I wan't them to randomly select a target from the array, which will contain me and all the other enemies.
Your answer
![](https://koobas.hobune.stream/wayback/20220613170723im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Remove first element of array 1 Answer
Find closest transform 1 Answer
Add one on local z axis 3 Answers
Closest array element 1 Answer
Particle System Delay 0 Answers