Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by pocikanec · Dec 07, 2010 at 05:20 PM · raycastobjectmousespawnclick

Mouse Click and Spawn object

Hello,

i got this script, but it don't work as i need..

var bednaPrefab : Transform;

function Update () {

var mousex = Input.mousePosition.x; var mousey = Input.mousePosition.y; var ray = camera.main.ScreenPointToRay (Vector3(mousex,mousey,0));

if ( Input.GetMouseButtonDown(0) ){ var crate = Instantiate(bednaPrefab, ray.origin, Quaternion.identity); }

}

it works, but it is spawning crate somewhere in space :D I have that script attached in Main Camera of 2D Platformer tutorial... So i need to make it like this:

You will play as normal. Then you click somewhere, and crate will spawns. It will get your mouse x & y (z = 0) and then spawns it. Crates are spawning, i can see, they are falling down, but i cant see the crates... Only in hierarchy. Help me! Any solutions?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by skovacs1 · Dec 07, 2010 at 05:37 PM

"it don't work" aside from being an abomination of the English language is also not very explicative. It tells neither how it failed to work nor in what context.

Instantiate does not take a ray as a position. You should try:

Instantiate(bednaPrefab, ray.origin, Quaternion.identity);

or more than likely

Instantiate(bednaPrefab, hit.point, Quaternion.identity);
Comment
Add comment · 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
0

Answer by pocikanec · Dec 07, 2010 at 06:03 PM

Hello sorry, but i haven't much time before...

now:

I want to spawn a crate - where i click with my mouse. I want only X a Y axis, Z will be 0 (side platformer game).

I want to click & then spawns a crate (in air) and then it falls.

So i attached that script to main camera & it gives me this error:

Assets/Scripts/qe.js(12,40): BCE0023: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEngine.Transform, UnityEngine.Ray, UnityEngine.Quaternion)' was found.

Comment
Add comment · Show 1 · 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 bjarnefisker · Dec 07, 2010 at 06:13 PM 0
Share

This error matches the answer written by skovacs1. You are feeding the Instantiate function with a Ray where it expect a Vector 3. Try one of the solutions given by skovacs1.

avatar image
0

Answer by pocikanec · Dec 07, 2010 at 06:31 PM

I have rescripted it:

var bednaPrefab : Transform;

function Update () {

var mousex = Input.mousePosition.x; var mousey = Input.mousePosition.y; var ray = camera.main.ScreenPointToRay (Vector3(mousex,mousey,0));

if ( Input.GetMouseButtonDown(0) ){ var crate = Instantiate(bednaPrefab, ray.origin, Quaternion.identity); }

}

Now it works, but it is spawning crate somewhere in space :D I have that script attached in Main Camera of 2D Platformer tutorial... So i need to make it like this:

You will play as normal. Then you click somewhere, and crate will spawns. It will get your mouse x & y (z = 0) and then spawns it. Crates are spawning, i can see, they are falling down, but i cant see the crates... Only in hierarchy. Help me! Any solutions?

Comment
Add comment · Show 3 · 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 pocikanec · Dec 07, 2010 at 08:32 PM 0
Share

please, help me!

avatar image Jesse Anders · Dec 07, 2010 at 09:40 PM 0
Share

You're posting your follow-up questions as answers to your original question. Please add additional information and any follow-up questions you have either as comments or as edits to your original post ins$$anonymous$$d.

avatar image pocikanec · Dec 07, 2010 at 10:22 PM 0
Share

edited... now it shows in first post

avatar image
0

Answer by Earth-O-Matic · Mar 31, 2011 at 05:26 AM

This is a very old question I know but maybe some one will find this helpful. This works for me...

Should work - attach to main camera

var bednaPrefab : Transform;

function Update () {

var mousex = Input.mousePosition.x; var mousey = Input.mousePosition.y; var ray = camera.main.ScreenPointToRay (Vector3(mousex,mousey,0));

var hit : RaycastHit;

if (Physics.Raycast (ray, hit, 200)) {

} if ( Input.GetMouseButtonDown(0) ){ var create = Instantiate(bednaPrefab, hit.point, Quaternion.identity);

}

}

Comment
Add comment · Show 1 · 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 TheCreatorultim · Sep 04, 2016 at 02:49 PM 0
Share

var mousex = Input.mousePosition.x; var mousey = Input.mousePosition.y; var ray = camera (Vector3(mousex,mousey,0));

has an error in it? for me is says: " is not possible to invoke an expression of type 'UnityEngine.Component'.

pls help

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

1 Person is following this question.

avatar image

Related Questions

Object Movement via Mouse Click? 3 Answers

Spawn Objects Where i click 2 Answers

How to make object clickable when user enter collider ? 0 Answers

How to attach objects using a mouse click on one side, and object only attaches to that one side? 0 Answers

Getting raycasting info from mouse event (eg through RaycastHit) 0 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