- Home /
How do I Spawn a random gameObject from Array1, at a random position of gameObjects from Array2 ?
My game is a board game (chess-like). The squares of the board that are empty, change during play, as my main object moves and eat various collectibles.
I have a script, that will spawn one random collectible in a random empty square, everytime I (manipulating my main object) eat a collectible.
This script will do it, using 2 Arrays:
1) the first Array, is this of the empty squares. It is actually an array of game objects. These game objects are empty game objects with a box collider, each of which sit on each one of the boards squares. Their tags change (full <-> empty) depending on whether there is a game object (my main game object or a collectible) on them, so this first Array updates each time I move my game object and is redifined, by the empty game objects with the empty tag.
2) the second Array is fixed. It is comprised of the collectibles that will randomly get spawned at one of the empty board squares, each time I eat a collectible.
How would I spawn a random game object from the 2nd Array, at one random position of the 1st Arrays empty game objects (that actually represent the empty squares of the board)?
So, Instantiate (WHAT, WHERE); ?
Answer by Loius · Jan 09, 2011 at 06:56 PM
var objectsToSpawn : Object[]; var spawnLocations : Transform[]; var spawnOffset : Vector3; // where to spawn the objectsw relative to the grid tiles
function SpawnOne() : GameObject { var thingToSpawn : int = Random.Range( 0, objectsToSpawn.length ); var placeToSpawn : int = Random.Range( 0, spawnLocations.length ); var newObject : GameObject = Instantiate( objectsToSpawn[ thingToSpawn ], spawnLocations[placeToSpawn].position + spawnOffset, Quaternion.identity ); return newObject; }
Thanks Vicenti, I'll try that now.
What is the use of "return newObject"?
It works. The "spawnOffset" was a nice touch. I didn't intend on using it at the beginning but it ended up really useful. I really appreciate your help :)
While it works, there is a problem: The newObjects are spawned as clones and their colliders don't trigger a tag change to the empty game objects that are located on each board square. Thus, the engine doesn't see the change in the empty positions of the board and the spawning can take place on occupied squares too. Does it have to do with the newObjects being "Clones"?
Everything is always spawned as a clone. :) That doesn't interefere with collision; whatever is buggy about collision is due to how you're handling it in the collided object. If you're checking "if .name == blah" then yeah, having Clone on the end will mess that up - but that's why SpawnOne returns the new object. You can change its name right after calling SpawnOne - var x = SpawnOne(); x.name = "BetterName"; or whatever. :)
Thanks Vicenti. I'm actually checking for the tag. I don't want to get into it in this question as I made a seperate question about the issue ("Instantiated (clone of) prefab, doesnt trigger another objects collider when inside it.") and I'm at the moment trying hard to figure it out. Your help was very big on this though, thanks again!
Your answer
Follow this Question
Related Questions
Simple array an spawning question 6 Answers
Random textures... 1 Answer
Calling random functions 4 Answers
Random select from array and spawn 1 Answer
How can i delete an item from an array after being used? (So, it won't be repeated) 2 Answers