- Home /
Easy question, deleting objects in order.
Hi
I have a bunch of spheres which get deleted OnMouseDown and on the final one you progress to the next part of the level, problem is you can just press the last one first and skip the entire thing. How do I add a prerequisite to the spheres so they can not be deleted until the ones prior to them have been already.
Thanks in advance.
Answer by robertbu · Feb 19, 2013 at 06:23 PM
Not really an easy question. There are several approaches you can take. One is to use a static variable and compare an id to the current count of remain objects. Here is the logic in C#:
public class DeleteInOrder : MonoBehaviour {
static int iCount = 5;
public int iID = 1;
void OnMouseDown()
{
if (iID == iCount)
{
if (iID == 1)
{
// Next scene code
}
Destroy (gameObject);
iCount--;
}
}
}
The iID needs to be 1 based, so in this case there are 5 objects with id values 1 - 5. The last one to delete will have the value 1. Note that iCount and iID can be set in code if you are dynamically creating the objects.
Ah ok, I thought it was going to be more of an 'if (other..blah)' easy answer. I write in Javascript so do you know of any decent way I can convert this? Thanks for your help robertbu.
For most scripts that come across this list, 90% of the conversion is changing how the variables are declared. Here is the same script in unity script:
static private var iCount : int = 5;
public var iID : int = 1;
function On$$anonymous$$ouseDown()
{
if (iID == iCount)
{
if (iID == 1)
{
// Next scene code
}
Destroy (gameObject);
iCount--;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613093011im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
OnDestroy() callback in Editor upon deleting selected GameObject - del key 5 Answers
Sorting an Array of GameObjects by Name 1 Answer
Change Mesh/Gameobject onmousedown?,Swap mesh/gameobject randomly onmousedown 0 Answers
I cant delete multiple instantiated prefabs 0 Answers
[SOLVE] 2D Space shooter - explosion power-up scripting, please help! 1 Answer