- Home /
Hi I am having trouble trying to destroy my enemy clones. How would I go about trying to do it with my enemySpawing and playerHitbox scripts.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class EnemySpawing : MonoBehaviour { public GameObject enemy; public float respawnTime = 1.0f; private Vector2 screenBounds;
// Use this for initialization
void Start()
{
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
StartCoroutine(enemySpawning());
}
// Spawns Enemys every one second.
private void spawnEnemy()
{
GameObject enemyClone = Instantiate(enemy) as GameObject;
enemyClone.transform.position = new Vector2(Random.Range(-screenBounds.x * 2, screenBounds.x * 2), Random.Range(-screenBounds.y * 2, screenBounds.y * 2));
}
// Starts spawning when game starts.
IEnumerator enemySpawning()
{
while (true)
{
yield return new WaitForSeconds(respawnTime);
spawnEnemy();
}
}
}
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerHitbox : MonoBehaviour { public GameObject enemy; private PlayerInfo playerInfo;
void Start()
{
playerInfo = this.GetComponent<PlayerInfo>();
}
// Destorys enemy
private void OnTriggerEnter2D(Collider2D other)
{
playerInfo.plasmaAmount += 1;
Destroy(enemy);
}
}
Answer by rhapen · Oct 31, 2020 at 11:50 PM
If i understand it right, In here i dont see you are actually referencing the object you hit
private void OnTriggerEnter2D(Collider2D other)
{
playerInfo.plasmaAmount += 1;
Destroy(enemy); //here you should do something like Destroy(other.gameObject);
}
Wow it was really that simple. I felt that I have been tricked by other people on this platform, but thank you very much.
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Removing gameobjects 3 seconds after spawning 3 Answers
How can i destroy spawned objects according a indicator? 2 Answers
Destroying GameController Respawns? 0 Answers
Losing references after reloading the scene, even though they exist again. 1 Answer