- Home /
Unity3d: Random.Range() Problem
I'm spawning objects from a list and so-far i got them to find a parent object that's already live in the scene.The problem is Random.Range() isn't working like I want. I want the listed Objects to spawn to a random parent, instead, they're spawning to the they're parent relative to the order of the list.
Ex. 0,1,2,3,4,5,6,7,8,9 = Bad
Ex. 8,3,1,4,6,3,7,9,5,2 = Good
lol
var theRange = Random.Range(obj1.Length,obj1.Length);
for(var i: int = 0; i < theRange; i++){
var obj2 : GameObject = obj1[i];
if(obj2.transform.childCount == 0){
objfromList.transform.parent = obj2.transform;
objfromList.transform.localPosition = Vector3(0,-2,0);
}
}
Deeply thankful
Answer by tanoshimi · Nov 01, 2013 at 09:17 PM
Your problem lies in your first line: the two parameters passed to Random.Range are a min and max value, and you're passing the same value for both - obj1.Length. So the only value that ever gets returned from Random.Range is obj1.Length itself.
You're right, and I did change it to Random.Range(0,obj1.Length); and there was no difference in the way they spawned. Plus I checked if childcount was 0. Also they're only ten parents and ten children so duplicating is killed off completely.
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Destroying Children Individually (Horrible..) 2 Answers
random limitation memory game 0 Answers
How to check parent value instead name value(more details in post) 3 Answers
Display Name Above Object 2 Answers