Question by
perf401 · Sep 24, 2021 at 07:19 PM ·
instantiateunity 2ddestroy objectclones
How to destroy an object clone?
Im too newbie atm and im struggling with simplest things. Basically arrow should be destroyed with OnCollision2D {Destroy(gameObject}; But in my case i made my original arrow get destroyed, and after it gets destroyed script just isn't working. So i want to destroy only arrow clones that getting instantiated from original arrow. But those arrow clones starting to exist only after getting instantinated during play, so i don't know how to make reference to them.
Arrow trap script:
[SerializeField] public Transform firepoint;
[SerializeField] private GameObject arrow;
[SerializeField] float timebetween;
public float starttimebetween;
private void Start()
{
timebetween = starttimebetween;
}
void Update()
{
if (timebetween <= 0)
{
GameObject arrowClones = Instantiate(arrow, firepoint.position, firepoint.rotation);
timebetween = starttimebetween;
}
else
{
timebetween -= Time.deltaTime;
}
}
}
And enemy projectile script:
public class EnemyProjectile : MonoBehaviour
{
public float speed;
Rigidbody2D rb;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
rb.velocity = transform.right * speed;
}
private void OnCollisionEnter2D(Collision2D collision)
{
Destroy(gameObject, 5f);
}
}
Comment