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 /
  • Help Room /
avatar image
0
Question by SmashDivine · Dec 30, 2016 at 08:01 PM · sprite2d gamemouseclickinstantiate prefab

Cannot instantiate 2D prefab on mouse click

Hello!

I'm working in 2D mode and I've got a prefab called "square" which is just a sprite of a square that I want to instantiate upon mouse click. I attached a script to my main camera with the following code:

 public Canvas canvas;
 public GameObject square;
 void Update () {
         if (Input.GetMouseButtonDown(0)) //Left mouse click
         {
            GameObject obj = (GameObject)Instantiate(square, 
            new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0), Quaternion.identity);
         }
     }

I attached my sprite prefab to the camera so that the GameObject square is referencing it. I've also tried hard coding the x and y values to values which I know should appear within the canvas. I should mention that the camera's z-coordinate is -10, less than the value of the instantiated object's z-coordinate.

So how can I get my sprite to appear?

Thanks!

EDIT:

Adding some screenshots. My Sprite: alt text

My Camera: alt text

EDIT:

So maca's answer below ended up being quite helpful. After some hours of testing and reading posts I found that what I was missing was the fact that ScreenToWorld doesn't work with a z-coordinate equal to that of the camera's (see this post). Setting the parent of my instantiated object to the canvas transform was also necessary (although I used the new SetParent() function), and I had to rescale my sprite a bit.

Thanks to the community and especially @maca for the help!

main-camera.png (40.6 kB)
sprite.png (29.4 kB)
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

1 Reply

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

Answer by LucianoMacaDonati · Dec 30, 2016 at 09:14 PM

If your object is a UI element then it must be under a Canvas parent. You could achieve this by following your code with obj.transform.parent = canvas.transform;

Comment
Add comment · Show 6 · 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 SmashDivine · Dec 30, 2016 at 09:17 PM 0
Share

Hey @maca, thanks so much for the reply! I did try this and it didn't seem to have any effect. Is there anything else I'm missing?

avatar image LucianoMacaDonati SmashDivine · Dec 30, 2016 at 09:19 PM 0
Share

Is your prefab a SpriteRenderer or an Image ? Also, remember that Input.mousePosition is in ScreenSpace, not WorldSpace. Camera.main.ScreenToWorldPoint(Input.mousePosition) could give you the correct position. If none of that helps, then I will need more information in order to help you =D

avatar image SmashDivine LucianoMacaDonati · Dec 30, 2016 at 09:23 PM 0
Share

Hello. $$anonymous$$y prefab is a SpriteRenderer. I just tried the suggestion you gave, but I got the same results. I think the coordinates are not the problem (or not the only problem) since using hard coded values also doesn't work.

For sure, what additional information can I provide that would help? Thanks so much for your assistance here!

Show more comments
Show more comments

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

108 People are following this question.

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

Related Questions

2d game - animation - drinking/glass 1 Answer

How do i make character eyes blink after some seconds have passed? (on 2D sprite) 1 Answer

My sprite size changes between animations? 4 Answers

is there a way to randomize the position transition of an object within a limited area? (using 2D sprite) 0 Answers

How could I do pixel art with no blurring rotation like in Tiny Survivor? 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