- Home /
Question by
Hamsterdogg55 · Jun 15, 2020 at 08:24 PM ·
instantiateprefabsinstantiate prefab
Destroying instantiated prefabs destroy all of my instantiated prefabs
I am making a top down shooter game and I want my enemy spawners to be able to be destroyed by the player. My issue is that when I destroy one spawner, the others are destroyed as well.
Here is my spawner Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class spawnSpots : MonoBehaviour
{
// Start is called before the first frame update
[SerializeField] GameObject[] enemy;
private float timeBetweenSpawns;
float startTimeBetweenSpawns;
[SerializeField] float minTimeBetweenSpawns = 1;
[SerializeField] float maxTimeBetweenSpawns = 3;
public bool isPlayerNear = false;
[SerializeField] float health = 10;
[SerializeField] GameObject deathParticles;
[SerializeField] GameObject[] paintSplatter;
Collider2D Mycollider;
// Start is called before the first frame update
void Start()
{
startTimeBetweenSpawns = Random.Range(minTimeBetweenSpawns, maxTimeBetweenSpawns);
timeBetweenSpawns = startTimeBetweenSpawns;
Mycollider = GetComponent<Collider2D>();
}
// Update is called once per frame
void Update()
{
if (isPlayerNear == true)
{
print("Spawning");
if (timeBetweenSpawns <= 0)
{
int randomEnemy = Random.Range(0, enemy.Length);
Instantiate(enemy[randomEnemy], transform.position, Quaternion.identity);
timeBetweenSpawns = startTimeBetweenSpawns;
print(randomEnemy);
}
else
{
timeBetweenSpawns -= Time.deltaTime;
}
}
else
{
print("Disabled");
return;
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (!CompareTag("PlayerSword"))
{
isPlayerNear = true;
print("true");
}
else
{
Physics2D.IgnoreCollision(FindObjectOfType<Projecile>().GetComponent<Collider2D>(), Mycollider);
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (!CompareTag("PlayerSword"))
{
isPlayerNear = false;
print("false");
}
else
{
Physics2D.IgnoreCollision(FindObjectOfType<Projecile>().GetComponent<Collider2D>(), Mycollider);
}
}
private void OnTriggerStay2D(Collider2D collision)
{
if (!CompareTag("PlayerSword"))
{
isPlayerNear = true;
print("true");
}
else
{
Physics2D.IgnoreCollision(FindObjectOfType<Projecile>().GetComponent<Collider2D>(), Mycollider);
}
}
public void TakeDamage()
{
health--;
print(health);
if (health <= 0)
{
print("Called");
int randomSpatter = Random.Range(0, paintSplatter.Length);
Destroy(this.gameObject);
var deathPart = Instantiate(deathParticles, transform.position, Quaternion.identity);
var paint = Instantiate(paintSplatter[randomSpatter], transform.position, Quaternion.identity);
Destroy(deathPart, 2f);
Destroy(paint, 5f);
}
}
}
And here is my Collision Script:
public class SpawnerCollider : MonoBehaviour
{
bool hit = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionEnter2D(Collision2D collision)
{
hit = true;
if(hit && hit == gameObject.GetComponent<Collider2D>())
{
FindObjectOfType<spawnSpots>().TakeDamage();
print("Collided");
}
hit = false;
}
}
Comment
Answer by ShadyProductions · Jun 16, 2020 at 05:27 AM
private void OnCollisionEnter2D(Collision2D collision)
{
var spawn = collision.gameObject.GetComponent<spawnSpots>();
if(spawn != null)
{
spawn.TakeDamage();
print("Collided");
}
}
Try this
Note that FindObjectOfType will just return the first object of that type in the scene that it finds, So this:
Physics2D.IgnoreCollision(FindObjectOfType<Projecile>().GetComponent<Collider2D>(), Mycollider);
Really doesn't look correct at all.
Also you probably want to compare tag with the object u actually collided with instead of the object the script is on instead?
if (!collision.gameObject.CompareTag("PlayerSword"))