- Home /
Assign the Instantiated Object to an Empty Object.
With this Script I made,I can Instantiate the Objects. The only problem is that it haven't been given a proper spawn location yet. I got some Empty gameobjects in the scene (the spawn location). My plan was when the Random.range(0,4)chose a number between 0 and 4 It would instantiate the object.This all works fine but as I mentioned I can't give it a proper spawn location :)
don't look at the if(GameObject.Find("Player").GetComponent(Rigidbody2D).isKinematic == false){ This was nessecary so that it wouldn't instantiate right from the start
//Several SpawnPoints
var Spawn1 : GameObject;
var Spawn2 : GameObject;
var Spawn3 : GameObject;
var Spawn4 : GameObject;
var Spawn5 : GameObject;
//The Obstacle
var Obstacles : GameObject;
//Repeats the function every second
InvokeRepeating("Start2", 0, 1.0);
function Start2()
{
if(GameObject.Find("Player").GetComponent(Rigidbody2D).isKinematic == false){
var Spawn = Random.Range(0,4);
if(Spawn == 0){
Debug.Log("spawn0");
Instantiate(Obstacles);
}
if(Spawn == 1){
Debug.Log("spawn1");
Instantiate(Obstacles);
}
if(Spawn == 2){
Debug.Log("spawn2");
Instantiate(Obstacles);
}
if(Spawn == 3){
Debug.Log("spawn3");
Instantiate(Obstacles);
}
if(Spawn == 4){
Debug.Log("spawn4");
Instantiate(Obstacles);
}
}
}
Thanks for reading, I hope you can help me out.
Answer by iamvishnusankar · Apr 30, 2014 at 04:52 PM
Declare the spawn points as array of objects and try rewriting your script as follows. This is C# version, Only some minor declaration tweaks would be required to convert it to JavaScript.
//Array of objects holds the spawn point positions
public GameObject[] spawnPoints;
public void Start2 ()
{
if(GameObject.Find("Player").GetComponent(Rigidbody2D).isKinematic == false)
{
//Call the function to instantiate at points
Spawn(Random.Range(0,spawnPoints.Length));
}
}
void Spawn (int i)
{
Transform currPos = spawnPoints[i].transform;
Instantiate (obstacles,currPos.position,Quaternion.identity);
}
Yeah it worked thx, It was a little bit confusing because I didnt understand why you made
Transform currPos = spawnPoints[i].transform
when I also just could do
Instantiate(Obstacles,Spawn1.transform.position,Quaternion.indentity);
Transform currPos = spawnPoints[i].transform
It deter$$anonymous$$es on which position the "obstacle" get spawned. Since, we have an array of SpawnPoints we need to deter$$anonymous$$e on which position the object should get Spawned. The "i" value selects a random SpawnPoint from array of gameobjects and the value is being assigned to a temp Transform(currPos). And this temp value is used to deter$$anonymous$$e on which position the Obstacles are instantiated.
So no need of writing like , Spawn1.transorm.position, Spaw2.transform.position etc . . as the above script will deter$$anonymous$$e the points by itself.
Well it works fine for me now with
Instantiate(Obstacles,Spawn1.transform.position,Quaternion.indentity);
So I wont change something that isnt broke,but thanks for the comment. It helped me understand more of the script. ;)
Your answer
Follow this Question
Related Questions
Enemy spawn script not working properly ! 1 Answer
instantiate a set amount 3 Answers
Issue With Spawning Enemies (javascript) 2 Answers
Machine gun script shoots all bullets at once 2 Answers
Script needs fixing ?! 1 Answer