Instantiate function in c#

hey everyone,

i’m trying to make a sphere appear form a blank game object called spwanPoint each time i click on the right arrow key. I know i’m supposed to use the Instantiate function but i’m a little confused… i created a prefab called touch, which is a sphere.

so far i have:

using UnityEngine;
using System.Collections;

public class touchIndicator : MonoBehaviour {
	
	// Use this for initialization
	void Start () {
			
	}
	
	// Update is called once per frame
	void Update () {

if(Input.GetKey("right")) {
		GameObject newObject  = (GameObject)Instantiate(Resources.Load("touch"), transform.position, transform.rotation);
         }
	}
	
}

thanks :slight_smile:
[/code]

I’d normally do this using the Editor:

using UnityEngine;
using System.Collections;

public class touchIndicator : MonoBehaviour {

   // this object will be available in the Inspector
   // to set via the editor

   public GameObject sphereToSpwan; 
   
   // Use this for initialization
   void Start () {
         
   }
   
   // Update is called once per frame
   void Update () {

if(Input.GetKey("right")) {
      GameObject newObject  = (GameObject)Instantiate(sphereToSpawn, transform.position, transform.rotation);
         }
   }
}

For your example, do you have a Resources folder under Assets?

// Instantiates a prefab at the path "Assets/Resources/enemy".
function Start () {
var instance : GameObject = Instantiate(Resources.Load("enemy"));
}

I do have a Resources directory where my prefab is stored. I’m a little confused as to what I assign to public GameObject sphereToSpwan. Is that where the prefab needs to be stored?

sorry, i haven’t been using c# for a while and am not very familiar with the Instantiate function…