I am using Unity and coding in C#. I have a game object called Enemy, which has a script called EnemyMotor. EnemyMotor has an int called ID. When EnemyMotor starts, I need to find an ID value that is not been using by any other instance of EnemyMotor.

For example: Enemy one spawns; check if an ID value of 1 is not being used. Enemy two spawns; check if an ID value of 1 is being been use. In this case, it is, so I need to find another ID value that is not being used. In this case, it should be 2. Continue with Enemy three, Enemy four and so on.

How do I go about ensuring a unique ID value?


This is fairly simple. Just have a static int to record the next ID, and increment it by one after you set it. All instances of the script will reference the same static int. When each new instance is created, it will run it's Start() method, set the nextID value as it's own, and ensure that the next nextID value is a value that has not already been used. Providing you only ever increment a value, it will never physically fall back on a previous value.

static int nextID = 0;
public int iD;

public void Start()
    iD = nextID;

IF you are instantiating your Enemies, you could put the "ID" in the name, so that in your Unity Scene Hierarchy you would see something like:


Then your script would check for the existence of an ID's enemie.

int my_check_id = 0;
bool check = false;
GameObject check_game_object;

while(check == false)
    my_check_id += 1;

    check_game_object = GameObject.Find("Enemie_" + my_check_id);

    if(check_game_object.name == null)
         check = true;

This code is untested, and not super efficient. If you need to check more than one property you may want to look at using a Singleton GameControl from the unity tutorials, and store into that and check it. But this should answer your initial question.

Let me know if you have any questions, or would like clarification drop a comment, I'm happy to update my answer!

  • 2
    \$\begingroup\$ This is kind of scary. If I have 100 enemies in my scene, adding a 101st checks all 100 enemies (plus anything else in my scene) 100 times each before it finds a valid ID, burning 10000 or more cycles through the Find loop (ie. it runs in O(n^2), quadratic time). We can do much better (O(1), constant amortized time) with a dictionary or set, or even simply a static counter. \$\endgroup\$ – DMGregory Feb 3 '17 at 22:17
  • \$\begingroup\$ The naming convention makes me cringe. \$\endgroup\$ – Gnemlock Feb 3 '17 at 22:19
  • \$\begingroup\$ Using strings as (unique) identifiers is slow and can cause difficult to debug problems \$\endgroup\$ – UnholySheep Feb 3 '17 at 22:26
  • \$\begingroup\$ @DMGregory You are correct, as I noted in the answer, this is not the most efficient approach. If efficeincy became a concern, then the proper solution would be to create a Persistent Singleton, and store values into an array much like the "GameControl" tutorial outlines on the Unity Tutorials. (Again, as I mention in the Answer). \$\endgroup\$ – Danoweb Feb 4 '17 at 22:36
  • \$\begingroup\$ @Gnemlock Yes, I agree with you.. the initial question, which now looks like it has been edited initially reffered to them as "Enemie" so I was sticking with that to reduce confusion, but I agree it's not great to look at. \$\endgroup\$ – Danoweb Feb 4 '17 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.