- Home /
Why could I delete my clones onlY in the same order of instantiation?
Hi! I am kind of new to Unity.
So, I've been trying to destroy set of clones on mouse click instantiated during my game play. My code for instantiation goes like this.
public class inst : MonoBehaviour { public GameObject ch;
public Transform[] spawnpoint=new Transform[3]; // Use this for initialization
void Start () {
spwn();
}
void spwn(){
for (int i=0; i<4; i++) {
Vector3 spawnVector = new Vector3 (spawnpoint[i].position.x, spawnpoint[i].position.y, spawnpoint[i].position.z);
Instantiate (ch , spawnVector, spawnpoint[i].rotation);
}
}
// Update is called once per frame
void Update () {
}
}
And, I have a separate code for destroying the clones on Mouse click which is added to the respective prefab.
But when I am trying to destroy them, they are getting destroyed in the same order in which they are created.
For eg., if I am clicking on my clone in spawnpoint[2] transform, spawnpoint[0] clone is getting destroyed. And on a second click of the same spawnpoint[1[]'s transform is being destroyed.
Please help me in making my code work properly. Thanks in advance :)
p.s.: I might have misunderstood and would have something wrong. I am just a beginner. In that case please correct me.
You've posted the wrong code here dude. We need to see the code which doesn't work.
I suspect you are incrementing an integer on each destroy or something to that effect.
meat5000
Thanks for replying dude. Btw my code is working. But its working in a wrong way
Anyway, this is my code to destroy the clones
public class onmouse : $$anonymous$$onoBehaviour {
// Update is called once per frame
void Update () {
if(Input.Get$$anonymous$$ouseButtonDown(0))/
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 10f))
Debug.DrawRay (ray.origin, hit.point);
Destroy(GameObject.Find(hit.collider.gameObject.name));
}
}
}
Thanks for the help in advance :)
Answer by CorruptedTNC · Mar 04, 2015 at 07:19 PM
Encapsulating properly shouldn't have actually fixed the problem (although it was definitely needed for readability). When you instantiate a GameObject from a prefab they all have the same name, which is their prefab name + "(Clone)". This is to help identify the ones you've spawned in the inspector and so on. When you use GameObject.Find it makes a list of all the gameObjects with that name and then picks the first on the list, which I believe is whichever one is first in the inspector. Since Destroy can only destroy 1 object at a time, it will always destroy the first object it finds with that name. The code you posted originally would have worked just fine, but with a small change.
public class onmouse : MonoBehaviour
{
void Update ()
{
if(Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 10f))
{
Debug.DrawRay (ray.origin, hit.point);
Destroy(hit.transform.gameObject);
}
}
}
}
hit.collider.gameObject should also have worked. The only problem with what you wrote was that you weren't specific enough with which GameObject you wanted to destroy.
Answer by meat5000 · Feb 28, 2015 at 08:00 PM
First, try extending your if statement brackets to encapsulate the Destroy routine.
if(mousebutton)
{
ray and hit setup
if(raycast)
{
Debug
Destroy
}
}
thanks for the help bro!! cheers :)
I have another doubt. pls help me understand this one. So I also tried to destroy my gameObject using On$$anonymous$$ouseDown() method. To my surprise the thing worked despite being a 2 code of just 2 lines.
So what is the difference between this On$$anonymous$$ouseDown() method and Physics.raycast() method.?
Quite a lot really :)
Rays can be used for all kinds of things within the physics system. On$$anonymous$$ouseDown can be more of a direct interface between an object and a mouse.
Your answer
Follow this Question
Related Questions
Destroy a specific instantiated clone? 2 Answers
Cloning Objects with Instantiate() - variables/references for added Components not stored? 3 Answers
Is there anyway to delete clones that are local variables? 1 Answer
[Solved]Instantiated item won't get destroyed until "picked up" again. 1 Answer
How can I destroy my Instance without renaming it? 2 Answers