- Home /
Destroying one of enemies stored in an array,
Dear All,
I am a complete beginner with basic knowledge of programming. The question concerns code at https://bitbucket.org/coderDarren/tappybird/src/master/FlappyBird/FlappyBird/Assets/scripts/Parallaxer.cs
Here, all enemies are generated from an original object at random positions and stored in an array. and the enemy that collides with the player (Player is also the tag used here) must be destroyed.
Normally, I would destroy as
void OnTriggerEnter2D(Collider2D col) { if (col.gameObject.tag == "ScoreZone") { OnPlayerScored(); Destroy(col.gameObject); } }
but here destroying the exact enemy that collided is becoming a challenge.
Bunch of thanks in advance.
For convenience, the code is also pasted here
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Parallaxer : MonoBehaviour
{
class PoolObject
{
public Transform transform;
public bool inUse;
public PoolObject(Transform t) { transform = t; }
public void Use() { inUse = true; }
public void Dispose() { inUse = false; }
}
[System.Serializable]
public struct YSpawnRange
{
public float minY;
public float maxY;
}
public GameObject Prefab;
public int poolSize;
public float shiftSpeed;
public float spawnRate;
public YSpawnRange ySpawnRange;
public Vector3 defaultSpawnPos;
public bool spawnImmediate;
public Vector3 immediateSpawnPos;
public Vector2 targetAspectRatio;
float spawnTimer;
PoolObject[] poolObjects;
float targetAspect;
GameManager game;
void Awake()
{
Configure();
}
void Start()
{
game = GameManager.Instance;
}
void OnEnable()
{
GameManager.OnGameOverConfirmed += OnGameOverConfirmed;
}
void OnDisable()
{
GameManager.OnGameOverConfirmed -= OnGameOverConfirmed;
}
void OnGameOverConfirmed()
{
for (int i = 0; i < poolObjects.Length; i++)
{
poolObjects[i].Dispose();
poolObjects[i].transform.position = Vector3.one * 1000;
}
Configure();
}
void Update()
{
if (game.GameOver) return;
Shift();
spawnTimer += Time.deltaTime;
if (spawnTimer > spawnRate)
{
Spawn();
spawnTimer = 0;
}
}
void Configure()
{
//spawning pool objects
targetAspect = targetAspectRatio.x / targetAspectRatio.y;
poolObjects = new PoolObject[poolSize];
for (int i = 0; i < poolObjects.Length; i++)
{
GameObject go = Instantiate(Prefab) as GameObject;
Transform t = go.transform;
t.SetParent(transform);
t.position = Vector3.one * 1000;
poolObjects[i] = new PoolObject(t);
}
if (spawnImmediate)
{
SpawnImmediate();
}
}
void Spawn()
{
//moving pool objects into place
Transform t = GetPoolObject();
if (t == null) return;
Vector3 pos = Vector3.zero;
pos.y = Random.Range(ySpawnRange.minY, ySpawnRange.maxY);
pos.x = (defaultSpawnPos.x * Camera.main.aspect) / targetAspect;
t.position = pos;
}
void SpawnImmediate()
{
Transform t = GetPoolObject();
if (t == null) return;
Vector3 pos = Vector3.zero;
pos.y = Random.Range(ySpawnRange.minY, ySpawnRange.maxY);
pos.x = (immediateSpawnPos.x * Camera.main.aspect) / targetAspect;
t.position = pos;
Spawn();
}
void Shift()
{
//loop through pool objects
//moving them
//discarding them as they go off screen
for (int i = 0; i < poolObjects.Length; i++)
{
poolObjects[i].transform.position -= Vector3.right * shiftSpeed * Time.deltaTime;
CheckDisposeObject(poolObjects[i]);
}
}
void CheckDisposeObject(PoolObject poolObject)
{
//place objects off screen
if (poolObject.transform.position.x < (-defaultSpawnPos.x * Camera.main.aspect) / targetAspect)
{
poolObject.Dispose();
poolObject.transform.position = Vector3.one * 1000;
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "ScoreZone")
{
//?
}
}
Transform GetPoolObject()
{
//retrieving first available pool object
for (int i = 0; i < poolObjects.Length; i++)
{
if (!poolObjects[i].inUse)
{
poolObjects[i].Use();
return poolObjects[i].transform;
}
}
return null;
}
}
Answer by BBIT-SOLUTIONS · Apr 06, 2020 at 11:13 PM
So you simply want to delete the gameobject inside an array? Then i guess somethink like this should work:
void OnTriggerEnter2D(Collider2D col) {
if (col.gameObject.tag == "ScoreZone"){
for(int i=0; i<poolObjects.Length; i++){
if(poolObjects[i].transform == col.gameObject.transform){
Destroy(col.gameObject);
}
}
}
}
I hope i understood it correctly how you use your PoolObject class.
You also could maybe use a List instead of an array, so that you have a data structure with dynamical length.
@BBIT-SOLUTIONS Thank you for your quick help. Your logic seems to be correct but the code does nothing. The code also does not seem to differentiate if the collision occurred against player or another of the enemies (if I am not wrong). I tried applying similar logic on the player before (just to test) but ins$$anonymous$$d of being destroyed, the player looses control and falls towards ground. After trying through Debug.log apparently the code section never runs. It does in the file for the player.
Done it from the player script The destroy method was not behaving understandably so I used void OnTriggerEnter2D(Collider2D col) {
if (col.gameObject.tag == "ScoreZone")
{
Vector3 newpos = col.gameObject.transform.position;
newpos.x = -9.7f;
col.gameObject.transform.position = newpos;
}}
Your answer
Follow this Question
Related Questions
Destroy by Contact (OnTriggerEntry) - destroying more then i want. 0 Answers
How come when the player runs into the coin it won't destroy? 1 Answer
Help with school project, collision, destroying objects 2 Answers
Unable to use other in OnTriggerEnter(Collider other) to destroy my respective objects 0 Answers
2d edge collider wont destroy? 0 Answers