Variable value in a GameObject name

I have a for loop that creates new gameobject with a name similar to “gameobjecti” with the i being the variable used in the for loop, so each would be named based on which loop created them, so gameobject1, gameobject2, etc.
Is this possible? how would I do this?

Are you asking for someone to write code for you or how to name a GameObject? Don’t know how to format a string? Which bit do you need help with?

You pass the name when you create the GameObject: Unity - Scripting API: GameObject.GameObject

for (var i = 0; i < 10; ++i)
{
    var go = new GameObject($"GameObject{i}");
      
    // other stuff...
}

Alternate way of formatting the string without using string interpolation shown above:

var go = new GameObject(string.Format("GameObject{0}", i));
3 Likes

If you simply write

gameObject.name = "GameObject" + i;

When you use + on two strings it will concatenate them into a single string with the contents of both strings put together.

If you use string + integer then the integer will be implicitly cast to a string, so it’s the same as string + string.

2 Likes

Thank you both!

1 Like