- Home /
Assign Names Problem
Hello , am having a really wiered problem with assiging names to prefabs am instantiating in the scene , each level I instantiate a number of objects in the scene and I specify a name for each , just using a for loop and adding i to the name of the objects, here is the code:
var to_load:Number; // 3
for(var i:uint ; i < to_load ; i++){
Instantiate (ball, Vector3(Random.Range(-3, 3), Random.Range(-3, 3), Random.Range(2, 8)), Quaternion.identity);
ball.name = "ball"+i;
}
everything is working perfectly except that if I change the variable to_load in the inspector and I play the game , it loads objects an skip somehow a number which causes to have an object without the correct name, but if I reload the level "without any modifications , just click stop then play again" it loads the objects as its suposed to , here are 2 screenshots to explain better:
This is First time load:
Second time load :
I tried many methods ,also to avoide the inspector but with no results , anyone has an Idea what could be causing this problem ? thank you
Answer by Kleptomaniac · Mar 07, 2012 at 08:13 AM
I'm not particularly sure what you're issue is (never used units before), however i changed some variables around and I believe this works. I basically got rid of your uint and instead declared to_load as an int:
var to_load: int = 3;
for(i = 0; i <= to_load ; i++){
Instantiate (ball, Vector3(Random.Range(-3, 3), Random.Range(-3, 3), Random.Range(2, 8)), Quaternion.identity);
ball.name = "ball" + i;
}
Hope that helps! Klep
hey , that does not really work for what am trying to do , but yes i fixed it just by assiging the name before instatiating , the problem might be that i come from a flash AS3 program$$anonymous$$g and it seems that unity has a diffrent way to assign names to the objects .
Haha, glad you got it working in the end! Sorry I couldn't be of more help!
Your answer
Follow this Question
Related Questions
Instantiate and then assign? 1 Answer
Issues with tracking prefab instances and gameobject naming (JS) 1 Answer
How to create a clone of an object with a different name 1 Answer
Instantiate only one Prefab per 40 unit 2 Answers
All subsequent instantiated objects = auto-named with suffix (Clone)? 1 Answer