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
1
Question by RealMTG · Dec 31, 2014 at 01:05 AM · uiinstantiate

Instantiate object on mouse click and when cursor IS NOT on UI

Hi!

I am working on some kind of Level Editor and I got a small problem. When you click on the UI to select the current object it instantiates the object anyways. How do I prevent this?

This is currently my code:

 if(Physics.Raycast(ray, out hit, levelEditorSettings.placementRange, levelEditorSettings.placementLayer))
         {
             hitPos = new Vector3(hit.point.x, hit.point.y, hit.point.z);
 
             var temp = hitPos;
             if(levelEditorSettings.enableGrid)
             {
                 temp.x = Mathf.Round (temp.x / levelEditorSettings.gridSize) * levelEditorSettings.gridSize;
                 temp.y = Mathf.Round (temp.y / levelEditorSettings.gridSize) * levelEditorSettings.gridSize;
                 temp.z = Mathf.Round (temp.z / levelEditorSettings.gridSize) * levelEditorSettings.gridSize;
 
                 hitPos = temp;
             }
 
             if(Input.GetMouseButtonDown (0))
             {
                 SpawnObject(spawnThisObject, hitPos);
             }
 
             if(editorMode == 0)
             {
                 DrawCurosr(spawnThisObject, hitPos);
             }
         }
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
2
Best Answer

Answer by Oliver1135 · Dec 31, 2014 at 01:08 AM

I had this problem earlier,

use EventSystem.current.IsPointerOverGameObject() to find if the mouse is over a UI element in which case you may want to return or do not instantiate

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 RealMTG · Dec 31, 2014 at 01:13 AM 0
Share

Thank you so much!

avatar image Kiwasi · Dec 31, 2014 at 01:15 AM 0
Share

This works. You can also implement a physics raycaster ins$$anonymous$$d of your own input code. See here for details.

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

26 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

Related Questions

Start() of Instantiated Prefabs Not Being Carried Out? 1 Answer

Populate UI Text with appropriate Network View 1 Answer

Creating Dynamic UI 0 Answers

How to insantiate an Inputfield which is populated with numerical values and to add the values of the field to get an output values? 0 Answers

Instantiated UI ignores parent transform on setactive method 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