creating object at coordinates when key is pressed

Is there a way to create an object at a set coordinates when key is pressed?

If so, what would the script look like?

Thanks

Let me know if you need any further help.

This is what I got:

var prefab : Transform;

function Start () { 
  prefab.active = false; 
} 

function Update () { 
   if (Input.GetKeyDown ("return")){ 
   if (prefab.active) 
   prefab.active = false; 
   else 
   prefab.active = true; 
   } 
}

for (var i=0;i<1;i++) {
    Instantiate (prefab, Vector3(-5.577429, 14.96738, -7.115993), Quaternion.identity);
}

The prefab is still visible at the start and when I hit return it says the prefab can not be made active.

I can’t say I understand this completely, but my intuition told me that you would need to treat the object that you drag from the Project view and the actual thing that it becomes in the game as two different things. It worked.

var prefab : Transform; //the prefab to be instantiated
static var object : Transform; //the instance of "prefab" that occurs in the game

function Start () {
	object = Instantiate (prefab, Vector3(-5.577429, 14.96738, -7.115993), Quaternion.identity);
	object.gameObject.active = false;
} 

function Update () {
	if (Input.GetKeyDown("return")) { 
		object.gameObject.active = !object.gameObject.active;
	}	
}

Going simply on what you described in your first post - creating an object when a key is pressed - that’s simply:

var prefab : Transform; 

function Update () { 
   if (Input.GetKeyDown ("return")){ 
    Instantiate (prefab, Vector3(-5.577429, 14.96738, -7.115993), Quaternion.identity); 
}
}

Thanks…That really helped!

No im having a problem making it so that the prefab will disapear and reapear insted of making more and more copies when the key is pressed more than once

thanks

GameObject.active will make something either be there or not be there. To do what you want to do, you can use StarManta’s code.

However, be aware that all the prefabs are going to be placed in the same location, using this code. If that is not what you want, let me know, and I’ll see what I can cook up.

just a tip… instead of hardcoding

Vector3(-5.577429, 14.96738, -7.115993)

create an empty that you can assign as a target. that way if you change your mind on spawn location you can easily move it in scene view. in your script you’d add…

var spawnTarget : Transform;

then in the inspector drag the empty on to the slot that reads Spawn Target.

and then it would be

Instantiate (prefab, spawnTarget.position, Quaternion.identity);