- Home /
[Solved] Make GameObjects spawn at custom locations (Slender like)
(Sry, maybe my English isn't perfect) Can I make multiple gameObjects spawn at custom locations, with custom rotation? Or is it easyer to put all of them to their location, but only activate 8 random picked ones when the scene starts?
I want to do that like in Slender (there are 10 possible locations but only 8 pages). Thanks!
Could you provide a code example?
or pseudocode at least
also if your native language is either spanish or german pm me and I can help.
Answer by N00MKRAD · Jul 14, 2014 at 02:53 AM
Ok, I found a really good script for it (AlucardJ´s Slender Guide)
You can set the spawnpoints (as much as you want) and set multiple pages (gameObjects, not prefabs) to spawn.
#pragma strict
import System.Collections.Generic; // to use List
public var spawnPoints : Transform[];
public var paperPrefabs : Transform[];
private var availableSpawnPointsList : List.< Transform >;
private var spawnList : List.< Transform >;
function Start()
{
ChooseRandomSpawnPoints();
SpawnPapers();
}
function ChooseRandomSpawnPoints()
{
var i : int = 0;
availableSpawnPointsList = new List.< Transform >();
spawnList = new List.< Transform >();
// add all spawn points to a list
for ( i = 0; i < spawnPoints.Length; i ++ )
{
availableSpawnPointsList.Add( spawnPoints[i] );
}
// choose a different random position for each paper prefab
for ( i = 0; i < paperPrefabs.Length; i ++ )
{
var rndPos : int = Random.Range( 0, availableSpawnPointsList.Count );
spawnList.Add( availableSpawnPointsList[rndPos] ); // add this to the spawn point list
availableSpawnPointsList.RemoveAt( rndPos ); // remove this from the available points list
}
}
function SpawnPapers()
{
// spawn paper at each location
for ( var i : int = 0; i < paperPrefabs.Length; i ++ )
{
var paper : Transform = Instantiate( paperPrefabs[i], spawnList[i].position, spawnList[i].rotation );paper.name = "Paper";
paper.name = "Paper";
}
}
It works fine, but I´ll try @MagicoCreator´s script, too.
Answer by MagicoCreator · Jul 13, 2014 at 10:54 PM
you can define an array of Vector3s (or GameObjects) and shuffle the array, and instanctiate at the first X elements with Random rotation: for example: GameObject[] Positions;
function Start(){
//Shuffle the array
for (int t = 0; t < Positions.Length; t++ )
{
GameObject tmp = Positions[t];
int r = Random.Range(t, Positions.Length);
Positions[t] = Positions[r];
Positions[r] = tmp;
}
//instantiate your objects
for(int i = 0; i < howmanyyouwanttoinstantiate; i++){
Instantiate(YourObject,Positions[i].transform.position,Random.rotation);
}
}
if you want to randomize the rotation only on Given axes you can use
Quaternion.Euler(Random.Range(0,360),Random.Range(0,360),Random.Range(0,360));
instead of Random.rotation, you can replace the Random.range with the number you want on the axis
Thanks! I found another good script at AlucardJ´s Slender Guide, but I´ll test which one is better.
Your answer
![](https://koobas.hobune.stream/wayback/20220613153913im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiating Multiple Game Objects to the Position of a Current GameObject 2 Answers
Best way to determine if there is a gameobject in multiple locations? 1 Answer
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Please help. How do I add a 3D Object at a specific location in the GO Maps package? 0 Answers