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 Major · Dec 19, 2012 at 12:38 AM · objectsgridsystemupgradeclicking

Clicking on objects

I am making a TD game and I need to be able to upgrade my turrets. Unfortunately I do not have the knowledge to do such a thing. Do note that I am NOT using a grid system. Any help is appropriated in advance. 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
3
Best Answer

Answer by Statement · Dec 19, 2012 at 01:02 AM

There's OnMouseDown, or you can do a ray cast when you click the mouse button to do stuff with the object that the raycast hit. Both solutions require a collider on the object. These are two ways of doing it that I think are pretty easy and popular.

OnMouseDown (easy):

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnMouseDown.html

Raycasting on click (pretty easy, requires more code, gives finer control):

http://docs.unity3d.com/Documentation/ScriptReference/Input.GetMouseButtonDown.html http://docs.unity3d.com/Documentation/ScriptReference/Input-mousePosition.html http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenPointToRay.html http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

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
2

Answer by aldonaletto · Dec 19, 2012 at 01:10 AM

Do you want to know how to click an object and replace it with another one? If so, use OnMouseDown in the object script:

 var nextObject: GameObject; // drag the replacement prefab here
 
 function OnMouseDown(){
   // create the new object at the same position and rotation
   Instantiate(nextObject, transform.position, transform.rotation);
   // destroy the older one
   Destroy(gameObject);
 }
Comment
Add comment · Show 9 · 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 Major · Dec 19, 2012 at 01:15 AM 0
Share

I want to just be able to select the object. Not replace it with anything. Also I only want to be able to select one object at a time.

avatar image aldonaletto · Dec 19, 2012 at 03:04 AM 1
Share

If you want to select one object at a time, use Raycast as @Statement said - for instance (script attached to the camera):

 var selected: Transform;

 function Update(){
   if (Input.Get$$anonymous$$ouseButtonDown(0)){ // if left button pressed...
     // create a ray passing through the mouse pointer:
     var ray = camera.ScreenPointToRay(Input.mousePosition);
     var hit: RaycastHit;
     if (Physics.Raycast(ray, hit)){ // if something hit...
       // if you must do something with the previously
       // selected item, do it here,
       // then select the new one:
       selected = hit.transform;
       // do whatever you want with the newly selected
       // object
     }
   }
 }
avatar image Major · Dec 19, 2012 at 03:14 AM 0
Share

Currently what happens is that whatever I hover over is selected, but not when I click. How would I achieve this? O am not an expert in this field of scripting. Thanks!

avatar image aldonaletto · Dec 19, 2012 at 03:32 AM 0
Share

$$anonymous$$aybe you're using my first version - I forgot to include the mouse button reading, which I fixed a few $$anonymous$$utes after. Take a look at the current code: it includes an enclosing if which only does the Raycast when the mouse button is pressed.

avatar image Major · Dec 19, 2012 at 03:36 AM 0
Share

Great! That works! And I can use GetComponent to make things happen right?

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

12 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

Related Questions

What causes the 'lag' I experience in Unity? 1 Answer

Grid Based Map Object-Array vs Arrays only 1 Answer

How to copy a particle system between objects? 1 Answer

Accessing local system ( File Browser ) 2 Answers

Positioning Objects Next to Each Other in Code 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