Incrementing gameObject names

I am working on a game that requires me to keep track of gameObjects by name. I have started coding a function for instantiating objects with a specific name but am getting errors on the instantiation process. Here is the code involved:

//identifying variables
var firstRoot: boolean = false;
var count: int = 0;
var pLocator: CharacterController = GetComponent(CharacterController);
var made: boolean = false;
var rootHandle: GameObject;

//other variables
var pointInstance;

function InstantiatePoint () {
 //Instantiate a new rootHandle with a name that is one greater than the previous rootHandle
 Instantiate (rootHandle, position, Quaternion.identity);
 count++;
 name = "rootHandle" + count;
}

The error I’m getting is BCE00005 Unknown Identifier : ‘rootHandle1’. which is because I am trying to go through some logic in other parts of my code with what would be the first gameObject instantiated.

The error says you’re referring to a variable that doesn’t exist. If you’re trying to make a variable called “rootHandle1” somewhere because an object is named “rootHandle1”, that’s not how it works, since variables aren’t strings. Probably you should be using an array rather than trying to refer to objects by name like this. Also, you’re changing the name of the object the script is attached to, not the instantiated object.

–Eric

Ok, but i want to create a game object with the name rootHandle1 and the variable for creating that game object to be rootHandle. How would I change the name of the instantiated object? If i was to do it as an array how would it work/look like? I’m generally really bad with arrays…

For your code, the solution is there:

function InstantiatePoint () {
  //Instantiate a new rootHandle with a name that is one greater than the previous rootHandle
  var newObject = Instantiate (rootHandle, position, Quaternion.identity);
  newObject.name = "rootHandle" + count++;
}

and for the array solution, it would look like this:

var listOfYourObjects = new Hashtable();

function InstantiatePoint () {
  //Instantiate a new rootHandle with a name that is one greater than the previous rootHandle
  var newObject = Instantiate (rootHandle, position, Quaternion.identity);
  newObject.name = "rootHandle" + count++;
  listOfYourObjects[newObject.name] = newObject;
}

ps: i used a hashtable so you could access items by name in the so called array… simply use listOfYourObjects[“rootHandleX”] and you’ll have you object

Thanks very much! I’ll try the other one first! I knew I was close to the solution aha.

K weird thing just happened. Firs tof all, thanks again for the solution. It worked. Secondly, I wrote my instantiatePoint function to run once in function start. I have commented out the rest of my script (update function and a move function) and I ran a debug.log to show me in the console what the name of each new object is. like so:

function Start () {
//make the first rootHandle
 InstantiatePoint(); 
}

function InstantiatePoint () {
 //Instantiate a new rootHandle with a name that is one greater than the previous rootHandle
 var newObject = Instantiate (rootHandle, position, Quaternion.identity);
 newObject.name = "rootHandle" + count;
 count++;
 Debug.Log(newObject.name);
}

But when i run the script it continuously creates new objects, all named rootHandle1. Why is it doing that when i only call instantiatePoint once and moreover why is it always naming it rootHandle1? it is as if the ENTIRE script reruns as if the program just started?

Did you put that script on the object you are instanciating ?

Add a Debug.Log(“Start”); in the start function, you’ll see if it gets called multiple times.

Yes it is getting called repeatedly.

And yes the script is on the object (which is a prefab)