How to spawn something where I click?

Alright, so I’ve looked around for the last hour trying to figure out the answer to the question and when I try to follow what other people are doing, Unity screams at me. So, here is my code right now:

using UnityEngine;
using System.Collections;

public class Instantiate : MonoBehaviour {
	//position and rotation can be equal to that of an object if you use the object's ID.

	public GameObject Sphere;
	public Transform Spawn;

	void Update ()
	{
		if (Input.GetButtonDown("Fire1"))
		{
			Instantiate(Sphere, Spawn.position, Spawn.rotation);
		}
	}
}

What changes do I need to make in order to make a “Sphere” copy spawn where I click?

Hi, You need to take a look at raycasting.

The most common raycast is used to draw a ray from the middle of the screen though the position of your mouse and then onto something ( maybe a floor or any other object)

what you want to do is use the position of where the ray hits the floor and use that point to instantiate your prefab to.

Here is a youtube link to what raycasting does and how its used


hope this is of any help.

I realize that the unity tutorial might be confusing since you are new to rayCasting.
I took some time to build a little scene where I have a plane, and everywhere I click appears
a cube(prefab).

here is the code I used along with some comments to help you understand what the code does :

using UnityEngine;
using System.Collections;

public class RaycastScript : MonoBehaviour{

Ray myRay;  	// initializing the ray
RaycastHit hit; // initializing the raycasthit

public GameObject objectToinstantiate;

void Update ()
{

	myRay = Camera.main.ScreenPointToRay (Input.mousePosition); // telling my ray variable that the ray will go from the center of 

// my main camera to my mouse (This will give me a direction)

	if (Physics.Raycast (myRay, out hit)) { // here I ask : if myRay hits something, store all the info you can find in the raycasthit varible.

// things like the position where the hit happend, the name of the object that got hit etc…etc…

		if (Input.GetMouseButtonDown (0)) {// what to do if i press the left mouse button

			Instantiate (objectToinstantiate, hit.point, Quaternion.identity);// instatiate a prefab on the position where the ray hits the floor.
			Debug.Log (hit.point);// debugs the vector3 of the position where I clicked

		}// end upMousebutton
	}// end physics.raycast	
}// end Update method

}// end class

note : make sure you have a floor or so that has a collider… The ray ignore everything that has no collider.
also if you instantiate a cube on the place the ray hits the plane, the cube will be half on top of the plane, half under it. a quick fix for that is to add a vector3 to the hit.point to make the cube rest on top of the plane . eg : instead of : Instantiate (objectToinstantiate, hit.point, Quaternion.identity); you might wanna do Instantiate (objectToinstantiate, hit.point + new vector3(0,0.5f,0), Quaternion.identity);

I hope I clarified the rayCasting a little bit and good luck with your project !