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(); //not necessary at the moment
GameObject.FindGameObjectsWithTag("prefabCube"); //not necessary at the moment
if (prefabCubeClone.GetComponent().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!
↧