• 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
Question by stylianos-theodorikakos · Sep 22, 2018 at 07:31 PM · unity 5scripting problemscript.click objectsclickable

Clickable object not properly working

Hello guys, so I have 3 clickable objects in my scene, they do the work that I want to be done but they are not properly working. What I mean but that, when I move my character and click it sometimes it does not work and you have to go further with the character and click it again. I also tried to make the sphere collider radius larger but it did not fix the problem. Here is my scirpt:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class ButtonOnClickActive : MonoBehaviour {
 
     public GameObject canvas;
 
   private bool clicked;
  
  void OnMouseOver()
   {
       if(Input.GetMouseButtonDown(0) && !clicked)
      {
          canvas.SetActive(true);
          clicked = true ;
           GetComponent<Renderer>().material.color = Color.red;
      }             
   }
 }
 


Any thoughts?

Comment

People who like this

0 Show 3
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 D3Duck · Sep 25, 2018 at 02:06 PM 1
Share

Bit hard to say. Is this the whole script? Are there any other scripts attached? Is the Collider ever deactivated? Try giving some more data on this as it should just work.

Most likely, some other collider is in between your view and the target so then there is no mouseover being registered by the collider you are trying to get. That would explain why moving handles is.

Try using some Debug.Log() statements in your OnMouseOver() function to see how often it is called (not including the if statement).

avatar image stylianos-theodorikakos · Sep 27, 2018 at 07:35 PM 0
Share

@D3Duck Yes it is the whole script. No other script attatched to the object and no the collider dont get deactivated. this is a print screen

link text

The clickable object is the one with the search icon...

avatar image KittenSnipes · Sep 27, 2018 at 11:31 PM 0
Share

@stylianos-theodorikakos

So you want it to work like a button? Or do you want it to be a clickable object that can only be clicked when the player faces it?

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by KittenSnipes · Sep 28, 2018 at 02:16 AM

@stylianos-theodorikakos Well I got a script that works. I explain what is going on through comments in the script. Hope it helps.

     [Header("Canvas related to clicked object")]
     public GameObject canvas;
 
     [Header("Needed to get the mouse position of the player")]
     public Camera cam;
 
     [Header("How far a player can hit an object from")]
     public float reachOfPlayer = Mathf.Infinity;
 
     //Variables:
     private bool clicked = false;
     private Color originalColor;
 
     private void Update()
     {
         //If left clicked
         if (Input.GetMouseButtonDown(0))
         {
             //Perform our click function
             ClickedOnObject();
         }
     }
 
     void ClickedOnObject()
     {
         //Will hold the reference for the clicked object
         RaycastHit hit;
 
         //Find the position and direction from the mouse
         Ray ray = cam.ScreenPointToRay(Input.mousePosition);
 
         //Using the direction and origin from the mouse(ray)
         //Output the hit object (out hit)
         //Max Distance the ray can go until it hits an object(reachOfPlayer)
         if (Physics.Raycast(ray, out hit, reachOfPlayer))
         {
             //If the hit object is the parent of this script and it has not been clicked
             if (hit.transform.gameObject == gameObject && clicked == false)
             {
                 //Turn on our canvas
                 canvas.SetActive(true);
 
                 //Make sure we set our object as clicked
                 clicked = true;
 
                 //Get our original object color
                 originalColor = GetComponent<Renderer>().material.color;
 
                 //Then change it to the selected color
                 GetComponent<Renderer>().material.color = Color.red;
             }
 
             //If the hit object is the parent of this script and it has been clicked
             else if (hit.transform.gameObject == gameObject && clicked)
             {
                 //Turn off our canvas
                 canvas.SetActive(false);
 
                 //Make sure we set our object as not clicked
                 clicked = false;
 
                 //Then change our object's color back to its original color
                 GetComponent<Renderer>().material.color = originalColor;
             }
         }
     }
Comment

People who like this

0 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 stylianos-theodorikakos · Sep 29, 2018 at 07:21 PM 0
Share

@KittenSnipes man thank you very much for your effort. Unfortunately it works like before it is like the character blocks the clicks

avatar image KittenSnipes stylianos-theodorikakos · Sep 29, 2018 at 09:14 PM 0
Share

@stylianos-theodorikakos

Because it does. What you want to do for that is either use a seperate layer to hold the player and have the raycast ignore the collision of that layer or you can use some other method :D

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

244 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 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 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

Attach to other coordinat SpringJoint2D from code. 0 Answers

Playmaker: How to use and get variables from other visual scripting or C#? 1 Answer

Non stopping point - counter 1 Answer

[Shooting shots Scripting] How to make the compiler know that I mean the empty quad by the Public Transform? 0 Answers

The spaceship acceleration script is good ? And how to use it with engine ? 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