- Home /
if chest has item, try next chest
i have 3 chests in scene and 3 prefab-items. every chest can carry only one item.
i will randomly instantiate the items to chests and if the chest already has one item, then the script must use next free chest.
i know how to place randomly items, but i don't know how to check the chest and if the chest has one item, how to try next chest?
my first part :
// my prefabs items var Item01 : GameObject; var Item02 : GameObject; var Item03 : GameObject;
// my empty chest positions in scene X, Y, Z var Chest01 : GameObject; // 0,0,0 var Chest02 : GameObject; // 2,0,0 var Chest03 : GameObject; // 4,0,0
function distributeItem() {
// create array of Items
var RandomChest = new Array();
RandomChest.Add(Chest01);
RandomChest.Add(Chest02);
RandomChest.Add(Chest03);
// create item in random chest
Instantiate(Item01, RandomChest[Random.Range(0,RandomChest.length)].transform.position, transform.rotation);
Instantiate(Item02, RandomChest[Random.Range(0,RandomChest.length)].transform.position, transform.rotation);
Instantiate(Item03, RandomChest[Random.Range(0,RandomChest.length)].transform.position, transform.rotation);
}
Answer by flaviusxvii · Mar 17, 2011 at 03:11 PM
Iterate through RandomChest until you find an empty chest.
Your answer
Follow this Question
Related Questions
Puzzle + Grid Instantiate - Random 2 Answers
Instantiate Random Object at Random Position 1 Answer
Making more than one item appear on a chess board 2 Answers
How to Instantiate only once? 2 Answers
Mulitple spawn points with random seed. 3 Answers