Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by warbeastGames · Mar 13, 2017 at 06:56 AM · c#spawnmouseposition

How do I spawn a prefab where I click?

what I mean is, I need to spawn a prefab where on the screen that I click, such as if I click on the upper right hand corner it should spawn there, as of now they spawn in the same place every time right above the player

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by dragonking300 · Mar 13, 2017 at 11:14 PM

 //Look at this script and learn from it
 
  using UnityEngine;
     using System.Collections;
     
     public class ClickSpawnScript : MonoBehaviour
     {
     
         public GameObject Prefab;
         public int RayDistance = 10;
         private Vector3 Point;
         public LayerMask Whatever;
     
         public void Update()
         {
             if (Input.GetMouseButtonDown(0))
             {
                 RaycastHit hit;
                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                 //Make Whatever a Raycast layer or if you don't want it just exclude it
                 if (Physics.Raycast(ray, out hit, Whatever.value))
                 {
                     Point = hit.point;
                     Instantiate(Prefab, Point, Quaternion.identity);
                 }
             }
         }
     }
          





Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image warbeastGames · Mar 14, 2017 at 01:26 AM 0
Share

Thank you, this is exactly what I needed

avatar image dragonking300 warbeastGames · Mar 14, 2017 at 02:45 AM 0
Share

NP, also, accept my answer so people don't see this

avatar image
1

Answer by SnStarr · Mar 13, 2017 at 07:09 AM

Do this..

     Public GameObject myPrefab; //Assign this in inspector or in code for whatever prefab you want instantiated.
         void Update()
         {
         if(Input.GetMouseButtonDown(0)) //If we click left mouse button
         { 
         Vector3 instantiatePosition = Input.mousePosition; //Setting a reference to our mouse position in a vector3
         Gameobject newGameObject = Instantiate(myPrefab, instantiatePosition, Quaternion.identity) //Creating a reference to our prefab and instantiating at the mouse position
 newGameObject.name = "MyObject"; //This will name the object instead of it instantiating as Clone
         }
     }




Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image warbeastGames · Mar 13, 2017 at 10:23 PM 0
Share

Thanks for the help but there's still a problem, it spawns them quite a ways off in the distance and not on screen, but it does actually spawn not just in the same space anymore

avatar image warbeastGames · Mar 13, 2017 at 10:51 PM 0
Share

@SnStarr Thanks for the help but there's still a problem, it spawns them quite a ways off in the distance

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Can you name something that you spawned with a spawner script? 1 Answer

(C#)Place GameObject from Mouse 0 Answers

Why Does My GameObject Not Spawn Anymore? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges