spawn a prefab

hi, how can i spawn a prefab where u push on the location of input.GetMouseButtonDown(0)

    //You can drag and drop your prefab into the inspector element that shows
    public GameObject Prefab;
  
    void Update()
    {
        if(Input.GetMouseButtonDown(0))
             Instantiate(Prefab); //This will instantiate the object
      
    }

Further information in instantiation:

However, you really should learn how to use google…

maybe but u should learn to read before :slight_smile: ā€œon the location of inputā€

SGM3 is correct, you should learn to read as well. http://bfy.tw/BP1W

1 Like

is not, the prefab spawn in the default location, maybe is correct but is not what i ask :slight_smile:

Wtf, dude… he tried to help you, even if he said ā€˜use google’, he was still trying to help. Next time a little more friendlier, please!

If you mean to instantiate a prefab at mouse position:

    public GameObject Prefab;
    void Update()
    {
        if(Input.GetMouseButtonDown(0))
             Instantiate(Prefab, Input.mousePosition); //This will instantiate the object at the mouse position
    }

If you need to instantiate it where a ray hits an object/a layer, then tell me.

1 Like

are u serius i didnt start ā€œHowever, you really should learn how to use googleā€¦ā€ xdd

That’s right, but if you use google you will find a lot of similar questions like yours which are already solved.

ty but i already try that and cant convert vector3 to transform

Because I am such a nice guy!

   private Vector3 _Position;
    public GameObject Prefab;

    void Update ()
    {
        if (Input.GetMouseButtonDown(0))
        {
            GetPosition();
            Instantiate(Prefab, _Position, Quaternion.identity);
        }
    }

    private void GetPosition()
    {

        Vector3 mousePosFar = new Vector3(
            Input.mousePosition.x,
            Input.mousePosition.y,
            Camera.main.farClipPlane);

        Vector3 mousePosNear = new Vector3(
            Input.mousePosition.x,
            Input.mousePosition.y,
            Camera.main.nearClipPlane);

        Vector3 mousePosF = Camera.main.ScreenToWorldPoint(mousePosFar);
        Vector3 mousePosN = Camera.main.ScreenToWorldPoint(mousePosNear);

        RaycastHit hit;

        if (Physics.Raycast(mousePosN, mousePosF - mousePosN, out hit, 1000))
            _Position = new Vector3(hit.point.x, hit.point.y, hit.point.z);

    }
1 Like

ty!!

Btw. for everyone else who only wants to spawn an object at mouse position:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class instantiate : MonoBehaviour {

    Vector3 mousePos;
    public GameObject Prefab;

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Vector3 mousePos = Input.mousePosition;
            mousePos.z = 10.0f;
            mousePos = Camera.main.ScreenToWorldPoint(mousePos);
            Instantiate(Prefab, mousePos, Quaternion.identity);
        }
    }
}