[SOLVED]Deletion of instantiated clones not in order
There is a similar question I did read through, but my situation may differ a little (not to mention I believe it was in java code and I am using c#). I am new to Unity (1 week in) and I am creating clones from a prefab correctly in the game using a Canvas button routing to a script in the empty game object for instantiation. So, I create them, and they work as individual gameobjects, correctly taking the instance codes etc from the prefab with one issue upon deletion.
THe issue: The clones delete in order, even when I point to accessible script variables for deletion.
I have both creation and deletion classes in one script so I don't have to access too much external object information. I know that with "Find" (appears to be any Find function), that they do in fact delete (and detect) in order of Instantiation. I understand that, but is there a way to delete out of order? I have drag and drop coding so the object is under the mouse when I execute the class code for deletion, but it still deletes the first one, and then nothing can delete after because it errors with the gameobject is missing (which I debugged and verified using createdObjects variables to show each one created in order).
My creation code:
public void CreateObj()
{
if (prefabCube != null)
{
Vector3 position = new Vector3((minX + maxX)/2, (minY + maxY)/2, -14);
//create the object
prefabCubeClone = Instantiate(prefabCube, position, Quaternion.identity) as GameObject;
createdObjects.Add(prefabCubeClone);
}
}
Under that, my deletion code:
public void DeleteOjb()
{
{
FindObjectsOfType<GameObject>(); //not necessary at the moment
GameObject.FindGameObjectsWithTag("prefabCube"); //not necessary at the moment
if (prefabCubeClone.GetComponent<DragDrop>().isActive == 1)
{
GameObject newClone = prefabCubeClone;
Debug.Log("WORKED");
Destroy(newClone);
}
}
}
So, I point to the clone(s) by name(i believe as a group Im not sure yet), then a script within them, and then a variable(isActive) in the script. isActive I have verified only can == 1 when the color changes upon drag and drop, and I did check that they change this variable indepenent also, so Im confused why when I say if isActive ==1 it does not understand the clone that actually has isActive==1 as the one I want to delete.
It appears that as long as I have isActive ==1 on ANY of them, it will still delete the first instance. I have heard of ray casting, and I tried that will no luck yet. I have also read a lot about colliders and With the executing code inside a Canvas button as an Event trigger (to outsource the execution), I do not know if that is the issue because it still works, just in order.
Thank you if you can help. I have been working on this part of the script for 2 days (12 hours average) testing different Unity tutorial and manual information but I cannot seem to get it right. I can delete a clone right now, and all scripting is working great other than needing to delete the right clone :P. I am unsure, other than instance variables (which I tried last today), how to reference them outside the order they instantiate for taking action in game.
Thank you and Bless you all!
Answer by sniper02311 · Jan 17, 2019 at 04:01 PM
[SOLVED] I figured it out.
What I did was take advantage of ray tracing at the axis the active camera was facing within 90 degrees either side and set the ray distance long enough to catch anything rendered in the viewpoint at a comfortable zoom factor. I debugged it to print the object name to make sure it found the right object and it works great. I now also use ray casting for collision detection without the need for mesh or rigidbody's since I am only using a custom editor scene for this part. Ill post details if anyone has the same issue.
Your answer
Follow this Question
Related Questions
destory object not variable 2 Answers
Destroy specific clone 3 Answers
[SOLVED] How to destroy a cloned object if it object colliding with another cloned object ? 1 Answer
Destroy function destroying only one object and not destroying his clones 0 Answers
How can you load next the level after you destroy the last clone c# 1 Answer