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 :slight_smile:

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.

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*.transform;*
  •  Instantiate (obstacles,currPos.position,Quaternion.identity);*
    
  • }*