Instantiating uniquely named objects

So my first major hurdle in September left me stumped for ages.

Highlighting some of the challenges I faced when creating Project 1.

Task

I was trying to instantiate a grid of button game objects. Whenever a button is pressed, the object is destroyed and after x seconds it’s recreated in the same position. I managed to get this working with a single object, but not a grid of them.

Problem

Nothing was getting recreated when I clicked a button in the grid.

I was giving every button in the grid the same name when it was instantiated. So when I tried to check if the object was destroyed there were obviously multiple more on screen so nothing would get recreated.

Tasks towards a solution

I needed to try to:

  1. Name each object individually!
  2. Check for each object to see if it was destroyed
  3. Recreate the destroyed object

Using the tag property

For task one, I tried tag every object, but learned that tags can only be created in the editor, are used mostly to group objects of the same type (like the same type of button) and they are static. They can’t be created dynamically from code.

Using the name property

Next, I tried to give each object an individual name using the Name property. This worked great. I used a variable to count how many objects were being created, then used that variable to construct a unique name for the object.

Task one solved!

Checking a list

For task two, I decided to create a list of the named objects. Then I looped through the list to see if any are found then return a bool of the result. I could then use this to decide whether to call the recreate object method. Nice idea, but unfortunately the way I called this method led to a loop of only seeing that the first object had been destroyed.

Back to the keyboard with that then.

Lessons learned

So far, these are my takeaways from my first challenge.

    • How to use tags appropriately
    • The Tag and Name properties of game objects

<!–

  • The difference between local position and world/global position.

–>

Obviously, I need to overcome task two so I can focus on implementing the rest of the gameplay and releasing this as soon as possible.