• 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 swifteria · Dec 24, 2014 at 08:02 AM · gameobjecttagsonmousedown

OnMouseDown on objects with tags

Hi all, I have here this script. using UnityEngine; using System.Collections;

 public class CutTree : MonoBehaviour 
 {
     public AudioClip treeChop;
 
     public Transform player;
 
     public float range;
 
     //Below is doing stuff concerning what happens when clicked and if it is in range.
 
     void OnMouseDown()
     {
         if(inRange())
         {
             audio.PlayOneShot(treeChop);
         }
         else
         {
             
         }
     }
 
     bool inRange()
     {
         if(Vector3.Distance(transform.position, player.position)<range)
         {
             return true;
         }
         else
         {
             return false;
         }
     }

I have assigned this to a tree in my game. If the player clicks on it, it plays a sound (but only if it is in range). It works fine. What I don't know how to do is assign this to the player, just for simplicity purposes and other things such as a woodCount or something. I was thinking of doing something with tags, such as

     void OnMouseDown(gameObject.tag="tree")
     {
     
     }
 
     

Also, just on a sidenote, on the 'else' statement underneath the PlayOneShot, how can I make some gui text pop up saying "Not in range"? Preferably also with a tag, so I dont have to always add a text for every time something else is clicked.

Thanks a lot!

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

3 Replies

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

Answer by swifteria · Dec 25, 2014 at 08:56 AM

Thanks to everyone who replied. I simply have used raycasts and GameObject names and attached this to my player script. Everything works fine, thanks very much!

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 Divinyx · Dec 24, 2014 at 11:50 AM

Perhaps make the tree dependent on the player? like

Tree:

  void OnMouseDown() {
       player.Cut(gameObject)
  }

Player

  void Cut(GameObject tree){
      if(inRange()) {
          //play tree's cut audio
      }
  }
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 SkaredCreations · Dec 24, 2014 at 12:01 PM

OnMouseDown is called when the user clicks on the object where this method is attached, so you cannot move this in your player script because of course it would happen when the user clicks the player itself (if it has a collider). Instead I'd suggest to add a method in your player script and call SendMessage, for example:

Player script:

 void DoStuff (GameObject go) {
    // Do something, for example:
    // if (go.CompareTag("myTag")) ...
 }

Tree script:

 void OnMouseDown()
 {
    if (inRange())
    {
       audio.PlayOneShot(treeChop);
       player.SendMessage("DoStuff", gameObject);
    }
 }

Alternatively, if you want one only place to catch all the mouse clicks then you'll need to use Physics.Raycast with the ray from the main camera to Input.mousePosition inside Update of your player script.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Reference creator of gameObject 1 Answer

Destroying Multiple Objects with Tags 1 Answer

Change Mesh/Gameobject onmousedown?,Swap mesh/gameobject randomly onmousedown 0 Answers

How would I add multiple gameobjects with different tags into one 1 Answer

How to calculate Distances 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