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 Herve_Simard · Feb 26, 2012 at 10:52 PM · selectionmouse click

Select something using mouseclick

Hello everyone,

I'm quite new with Unity and am starting to build a tower defense.

I've set up a few things but now I wonder how I might select something.

I need, during the game, to be able to select something and make it do something (ex: select a tower and make it aim at a certain place). But i've yet to figure out what to do and how to do it. I fugure I'd need to use MonoBehaviour (already using this), but that's as far as I got.

Anyone can give me any info on that? Thanks!

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
0

Answer by Tommynator · Feb 27, 2012 at 02:18 AM

The OnMouseDown event call of the MonoBehaviour class is your friend ;)

Simply attach a collider and a script to your tower object and implement the OnMouseDown function.

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 Herve_Simard · Feb 27, 2012 at 03:28 PM 0
Share

Thanks, I've just read a few things following your suggestion. Problem is, now, that I can't fathom how to use it. Any ideas?

avatar image
0

Answer by 1337GameDev · Aug 26, 2013 at 02:41 AM

I would use the onMouseDown method to handle the mouse clicks for the object. Make a script that has a method void OnMouseDown() and then do the selection from the object script. Your object must have a collider on it however for this to work.

so:

void OnMouseDown() { //script this is in, is attached to the object you want to be selectable

//add object this script is attached to your list of selected objects //instantiate a prefab that displays the object is selected.

}

I would use a selection manager singleton to do this however. I have my singleton just do a single raycast using a mouse click to hit an object with a collider, check if i can select it, instantiate the prefab, and add it to its list of selected objects.

I also take into account if this item is already selected by checking for the prefab (by name) as a child of the root gameobject of the unit, and deselecting it.

This seems to work just fine for me, as you won't be selecting more than one object at a time with this, so a raycast is fine.

Hope i've helped.

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

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

6 People are following this question.

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

Related Questions

How to make a mouse click selection for in game use 1 Answer

[SOLVED]Object selection scrip doesn't work. 2 Answers

Selection script and text box input 1 Answer

Make a turret shoot multiple targets 1 Answer

RTS Style Selection system (How to control one unit and not the others) 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