Make item spawn at a empty game object location.

i want the item to be be spawned at the location of a empty game object i have the variables all ready made if u need the whole script comment and ill post it

      	function Update ()
      	{
      			       if (drop)
	       {
	       	Instantiate (clone);
	       	Instantiate (clone1);
	       	drop = false;
	       	Destroy (pick);
	       }
	    }

3 Answers

3

You have to use Instantiate overload and provide position and location of the instantiated object. Assuming emptyObject is a GameObject, you can use:

Instantiate(clone, emptyObject.transform.position, Quaternion.identity);

it works but im wondering if there is a way to make it the player it spawns on? i tried it works true but the prefabs i have set wont accept the player i have in the game granted it does 100% work im just wondering if there is a way to make it so were ever my character is the cube spawn there? ps- if u dont know thats fine im just wondering if there is a way?

If I understand you correctly, you now want to spawn your object at the location of the player. It should work just fine, if you put your Player position as the second argument, e.g. Player.transform.position (assuming Player is a GameObject).

// Create an empty object in scene
// Attach this script to any object in scene
// See: emptyObject.transform.position, emptyObject.transform.rotation
// Location and rotation are params 2 & 3 of Instantiate()

var emptyObject : Transform; // Drag empty object here

var drop = false;

var clone : Transform; // Drag your clone obj here

function Start() {
	drop = true;
}

function Update ()
{
	
	if (drop)
	{
		Instantiate (clone, emptyObject.transform.position, emptyObject.transform.rotation); 
		
		drop = false;
		
	}
}

Alright but what if there’s instances for spawn points how do I get the items to spawn at the locations these spawn points are located at?

public float spawnTime;
    private GameObject respawnPrefab;

	// Use this for initialization
	void Start () {
        respawnPrefab = GameObject.Find("BallSpawnPoint");
        InvokeRepeating("BallSpawn", 0, spawnTime);
	}
	
	void BallSpawn () {
        var ball = Resources.Load("Ball");
        Instantiate(ball, respawnPrefab.GetComponent<Transform>().position, respawnPrefab.GetComponent <Transform>().rotation);
	}