- Home /
Moving a specific gameobject in a list
As the titles says, i'm trying to move a specific gameobject in my list. What my code ends up doing is only moving the first gameobject added to the list. Here's my code.
public List<GameObject> Enemies = new List<GameObject>();
public GameObject Enemy;
public float TimeLimit;
public float Timer;
public float Speed;
public int count;
void Update ()
{
Timer++;
if (Timer >= TimeLimit)
{
Enemies.Add(Enemy);
GameObject.Instantiate(Enemies[count], Enemy.transform.position, Quaternion.identity);
count = count + 1;
Timer = 0;
}
if (Enemies.Count == 5)
{
Enemies[2].transform.Translate(Vector3.up * Speed * Time.deltaTime);
}
}
Anyone have any suggestions? Thanks for reading!
Answer by whydoidoit · Oct 17, 2012 at 11:41 PM
You want to do
Enemies.Add(Instantiate(Enemy, Enemy.transform.position, Quaternion.identity));
The count will be Enemies.Count
It gives me two errors :( "cannot convert `UnityEngine.Object' expression to type `UnityEngine.GameObject'" and The best overloaded method match for `System.Collections.Generic.List.Add(UnityEngine.GameObject)' has some invalid arguments
Sorry - that's:
Enemies.Add(Instantiate(Enemy, Enemy.transform.position, Quaternion.identity) As GameObject);
Great news! Glad you got it working.
Can you mark is as answered by clicking the tick? $$anonymous$$eeps the board clean.
Your answer
Follow this Question
Related Questions
Subtracting the position of transform from the position of Game Objects in a list. 1 Answer
How to select an Game Object from an Item List 0 Answers
C# List foreach problem 1 Answer
How to select the Nth gameObject in a list and put it into a gameObject variable 1 Answer
Keep list of GameObjects between scenes 2 Answers