Instantiate given object on an empty game object.

Hello, I’ve been trying to spawn an object using Instantiate and place it on the location of an empty game object. Here is my script, it works fine but it instantiate the object at the center of the scene (I want to spawn the source object on the spawnPoints instead).

var source : GameObject;
public var spawnPoints : GameObject[];

function Start () 
{
		
var source = Instantiate(source, transform.position,transform.rotation);
    	
}

1 Answer

1

var source : GameObject;
var spawnPoints : GameObject;

function Start () 
{
var pos : Vector3 = spawnPoints[Random.Range(0, spawnPoints.Length)].transform.position;
var instance = Instantiate(source, pos ,transform.rotation);
}

Select a random spawnpoint and spawn your source there.

I am getting the error "';' expected. Insert a semicolon at the end." On the line with Vector3 pos = spawnPoints[Random.Range(0, spawnPoints.Length)].transform.position;

Woopsie. Mixed C# with UnityScript. Fixed now

Thanks, it's working perfectly !