The question is answered, right answer was accepted
How do I destroy a prefab from an array list?
Hi!
These are my declarations:
GameObject player, HitObject, pacmanLives, lifeClone, orangeGhost, redGhost, pinkGhost, blueGhost;
Vector2 firstLife = new Vector2(-17f, -13f);
public List<GameObject> Occurrences = new List<GameObject>();
public bool canKillGhost = false;
void Start () {
FindObjects();
SpawnLives();
}
void FindObjects () {
Player = GameObject.Find("Player");
pacmanLives = Resources.Load("Prefabs/PacManLife") as GameObject;
orangeGhost = GameObject.Find("OrangeGhost");
redGhost = GameObject.Find("RedGhost");
pinkGhost = GameObject.Find("PinkGhost");
blueGhost = GameObject.Find("BlueGhost");
}
I am currently spawning 4 instances of the same prefab using this script:
void SpawnLives() {
for(int i=0; i<4; i++) {
firstLife.y += 4.5f;
lifeClone = Instantiate(pacmanLives, firstLife, Quaternion.identity);
Occurrences.Add(lifeClone);
}
}
This part is working as when I press play, all 4 prefabs gets instantiated in the position I want them to.
Now my problem is here:
void DecreaseLives() {
int lives = Occurrences.Count - 1;
while (lives >- 1) {
//HitObject = null;
Destroy(Occurrences[lives]);
Occurrences.RemoveAt(lives);
lives--;
}
}
What I need the script to do is whenever any of the ghosts that are tagged as "aiAgent" from Unity's UI collide with the player, I need one life to decrease.
This is the collision code:
private void OnTriggerEnter2D (Collider2D collision) {
HitObject = collision.gameObject;
}
private void OnCollissionEnter2D (Collider2D collision) {
HitObject = collision.gameObject;
}
void FixedUpdate () {
if (HitObject != null && HitObject.tag == "aiAgent" && canKillGhost == false) {
DecreaseLives();
Debug.Log("Test Hit");
}
Note that this script is attached to the player and both the ghosts and the player have a Box Collider 2D. Finally, the player has a Dynamic Rigidbody 2D and the ghosts have a Kinematic Rigidbody 2D.
Thanks!
I forgot to state that it is not registering the collision - so this might be the problem. Therefore the Debug.Log("Test Hit");
is not being outlined on runtime.
Answer by Vendrict · Jan 26, 2019 at 11:34 AM
Okay, a user by the name of Pulni#5958 from the http://discord.gg/gamedev Discord channel gave me the solution.
This is what I had wrong:
I had to change this:
private void OnCollissionEnter2D (Collider2D collision) {
HitObject = collision.gameObject;
}
to
private void OnCollissionEnter2D (Collision2D collision) {
HitObject = collision.gameObject;
}
Afterwards, I had to tick the Is Trigger from Unity's UI for the ghosts.
Then, I had to uncomment the HitObject = null
.
Finally I reverted the while loop to and if, else if:
int lives = Occurrences.Count - 1;
if(lives == 0) {
Debug.Log("No more lives are left. You lose");
}
else if (lives > -1) {
HitObject = null;
Destroy(Occurrences[lives]);
Occurrences.RemoveAt(lives);
new WaitForSeconds(1.5f);
}
Follow this Question
Related Questions
Destroying a spawned prefab instance 0 Answers
2d collision not functioning correctly 0 Answers
Destroying Instantiated Prefabs From A Different GameObject 1 Answer
destroying a collected coin 2 Answers