x


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: alt text

Second time load : alt text

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

more ▼

asked Mar 06 '12 at 11:29 PM

dohmee44 gravatar image

dohmee44
1 1 2 2

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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

more ▼

answered Mar 07 '12 at 08:13 AM

Kleptomaniac gravatar image

Kleptomaniac
2.5k 6 11 21

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 programming and it seems that unity has a diffrent way to assign names to the objects .

Mar 07 '12 at 08:16 AM dohmee44

Haha, glad you got it working in the end! Sorry I couldn't be of more help!

Mar 07 '12 at 08:18 AM Kleptomaniac
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2086
x1672
x143
x92
x29

asked: Mar 06 '12 at 11:29 PM

Seen: 390 times

Last Updated: Mar 07 '12 at 08:18 AM