- Home /
Clone of rigid body prefab destruction if at certain position
Not getting my mind wrapped around this despite reading the docs a few times (maybe not reading the right one?).
I have a rigidbody prefab that I clone many times on mouse down.
If any of the clones is at a certain position I want to destroy that clone only.
Here is what I have so far but the clone that satisfies the condition does not get destroyed and in fact I have a null reference on line where I'm checking the position.
using UnityEngine;
using System.Collections;
public class MainLoop : MonoBehaviour {
public Rigidbody projectile;
public GUITexture newCloneButton;
Rigidbody clone;
void Update() {
if (Input.GetKeyDown("up")) {
clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
clone.position = new Vector3(0.0F, 10.0f, 3.0f);
}
if(clone.rigidbody.transform.position.y < -1.0f ){
Destroy(clone);
}
}
}
Any help is appreciated.
Answer by Berenger · Jan 12, 2012 at 11:38 PM
Try to declare your clone as a GameObject instead, then use clone.transform.
An other solution if you want your clone to be destroyed if it's too low, you can put a box collider at (0, -10, 0), expand it's width and length, then add a script which destroy everything in OnColliderEnter(). If you want the same on each side, you need the box to contain your whole scene, then override OnColliderExit instead.
Answer by robbiek · Jan 13, 2012 at 01:22 AM
Have you simply tried Destroy(gameobject); instead of clone?
I haven't tried but I will just to see if there is more than one way to do this. Thanks.
Answer by hunter789 · Jan 18, 2012 at 02:17 AM
I'm new to unity (not to programming/game programming) but I've noticed this:
- You create many clones (one per frame is the mouse is down), but you keep track of only one.
- You check only if the last clone has to be deleted, whether it is this same frame or a previous one.
- If the last clone created fulfills the condition and is destroyed, and no more clone is created just the frame after, your clone is a destroyed object.
I haven't tested it in Unity, but wrote it in visual studio, so there should be no syntax errors:
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class MainLoop : MonoBehaviour { public Rigidbody projectile; public GUITexture newCloneButton; List<Rigidbody> clones; // keep track of ALL your clones
void Update()
{
List<Rigidbody> removeItems; // needed for clean up
if (Input.GetKeyDown("up"))
{
Rigidbody clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
clone.position = new Vector3(0.0F, 10.0f, 3.0f);
clones.Add(clone);
}
// http://stackoverflow.com/a/4868062/637987
removeItems = new List<Rigidbody>();
foreach (Rigidbody clone in clones)
{
if (clone.rigidbody.transform.position.y < -1.0f)
{
removeItems.Add(clone); // make a list of all the clones you have to destroy
}
}
// Destroy all the clones that have been flagged
foreach (Rigidbody clone in removeItems)
{
Destroy(clone);
clones.Remove(clone);
}
}
}
You can't remove the clones directly in the main list because if you remove an element while you look through the list, you change it's composition and behavior gets messed up (in short, it blows up :P). This is why you first have to make a new list, then with the new list modify the first one.
I hope it helps :)