How can I assign a unique int to all GameObjects in array?

How can I set a unique int to every GameObject in my array?

public GameObject[] targets;

private void Update()
{
targets = GameObject.FindGameObjectsWithTag("Enemy");
}

Just assign the index in the array. Guaranteed to be unique.

1 Like

They already have a Unique ID! GetInstanceID:

private void Update()
{
    targets = GameObject.FindGameObjectsWithTag("Enemy");
    foreach(var t in targets) 
    {
        Debug.Log(t.GetInstanceID());
    }
}

That’s guaranteed to be the same thing no matter where you look at the object from.

1 Like

Thanks this worked just fine :slight_smile:

Thank you :slight_smile: I used the array index as @Kiwasi suggested! :smile: