- Home /
Question by
Manacraft05 · Dec 28, 2018 at 09:35 PM ·
arraydestroy objectarray of gameobjectsgameoverrestart game
Delete Instance.
Hey, I want to destroy the oldest entry of a array of gameobjects. how to ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class killer : MonoBehaviour {
public Rigidbody2D collider2;
public player script;
public SpriteRenderer spriteRenderer;
public Sprite sprite;
public Collider2D collider1;
// Use this for initialization
void Update () {
}
void Awake()
{
DontDestroyOnLoad(this.gameObject);// to keep the player ragdol when die
}
private void OnTriggerEnter2D(Collider2D collision) // when hit the ground
{
if (collision.tag == "ground")
{
script.enabled = false; // changes of sprite ect...
spriteRenderer.sprite = sprite;
collider2.constraints = RigidbodyConstraints2D.None;
collider1.enabled = false;
Application.LoadLevelAsync(Application.loadedLevelName); // reload the level
}
}
}
Comment
Answer by ray2yar · Dec 29, 2018 at 12:59 PM
Create a custom class that consists of a game object and a float/int to represent the time it was placed into the array. Then iterate through the array to find the gameobjecr with the smallest time value.
Your answer

Follow this Question
Related Questions
why if loops doesnot work? 1 Answer
Using an int to find the index of a GameObject array 1 Answer
Accessing next Camera from array 1 Answer