• 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 tanavya · Oct 19, 2016 at 09:52 AM · gameobjectbuttonbutton trigger eventsonclick

On Click Add Listener not working on Game Object

I have this code attached to a 3D cylinder game object. I plan to use it as an arrow and rotate it when clicked and dragged. I had no idea what do as I am a beginner so I started with adding an onClick listener to a Button component on the Cylinder, and wrote this script which I added as component to the cylinder.. But the Rotate() function isn't called when I click on it, as there is no message in the Log.. This is the code:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class MoveAim : MonoBehaviour {
 
     public bool isAiming;
 
     public float rotationSpeed = 50.0f;
     private float maxLeftAngle = 85.0f;
     private float maxRightAngle = 275.0f;
     private Button button;
 
 
     void Start () {
         isAiming = true;
         button = GetComponent <Button>();
         button.onClick.AddListener (Rotate);
         Debug.Log ("Start!");
     }
 
     void FixedUpdate () {
     }
     void Rotate() {
         Debug.Log ("Click recorded");
     }
 
 }

Any idea what to do? And how should I implement the rotation of the cylinder? Help would be appreciated!

Thanks!

Comment
Add comment · Show 1
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 fafase · Oct 19, 2016 at 01:02 PM 0
Share

Unfortunately, you are heading wrong. See the button is not a way to detect interaction on an object. You would use On$$anonymous$$ouseXXX methods for that. Button component is a UI item so it should be used within a Canvas.

I won't go any further as there is too much to say on that topic.

Basically, you don't need the Button there. You need to detect mouse/touch movement and use the delta value to perform the rotation.

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by LynoHD · Oct 19, 2016 at 11:46 AM

as far as im concerned UI.Buttons needs to be inside of a lambda function. try doing this instead and no, do not use this in an update.

 void Start()
 {   
 button.onClick.AddListener( () =>
     {
     Rotate();
     });
 }

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 Zodiarc · Oct 19, 2016 at 12:05 PM 0
Share

Actually

 button.onClick.AddListener(() => Rotate());

At least I used it like that.

avatar image LynoHD Zodiarc · Oct 19, 2016 at 12:09 PM 0
Share

Yeah that probadly works too, lol. I just always use brackets

avatar image
0

Answer by Naphier · Oct 20, 2016 at 04:07 AM

Buttons need to be under a UI Canvas and there must be an event system in your scene (Just right-click and create UI button and you'll see what all needs to be added). That's if you actually want this to be controlled by a button. If not then you need to add a collider trigger to your game object and in the script attached to the game object you'll use one of the OnMouseXXX Monobehaviour events, likely OnMouseDown to start rotating an OnMouseUp to stop rotating.

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

Answer by SeanKilleen · Oct 19, 2016 at 10:39 AM

You should use the void Update() method to check for button clicks for example:

 void Update(){
       if (Input.GetMouseButtonDown(0))
                  Rotate();
                  Debug.Log("Rotate Started");
 }
Comment
Add comment · Show 5 · 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 danivdwerf · Oct 19, 2016 at 12:21 PM 2
Share

He should definitely not do that.

avatar image SeanKilleen · Oct 19, 2016 at 12:49 PM 0
Share

@danivdwerf can you please explain why he definitely shouldn't do that. I am using similar functionality in my own game so it could be beneficial to know what is the problem with this approach. I'm also very new to game development so while I can write code well, I don't actually know video game best practices etc. Any help would be appreciated.

avatar image fafase SeanKilleen · Oct 19, 2016 at 01:03 PM 1
Share

It is not entirely wrong considering the OP is doing totally wrong. Though that won't solve entirely since it won't make sure you are clicking on the item, it is already a first step.

avatar image danivdwerf SeanKilleen · Oct 19, 2016 at 05:38 PM 1
Share

@Sean$$anonymous$$illeen, first of all, he wants the button input and not if the mouse is pressed. Also, you only have to tell the button once that he has to listen. Else every update, you tell your button to listen to a click.

avatar image Naphier danivdwerf · Oct 20, 2016 at 04:09 AM 1
Share

Also the Debug,Log is not in braces so it's going to be called every damned frame! Even if it were, holy console spam, Batman!

avatar image
-1

Answer by danivdwerf · Oct 19, 2016 at 12:20 PM

Hey man, try this:


button.onClick.AddListener(delegate(){Rotate();});
Also, I'm pretty sure Unity does not like it when you put a function in a function, It seems you put the Rotate function in the FixedUpdate function. If you want it to happen multiple times, you should use an coroutine. If you are not familiar with it, I'm happy to help

Good Luck.

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 LynoHD · Oct 19, 2016 at 12:23 PM 0
Share

what exactly does the delegate do? I havent gotten into delegates yet

avatar image danivdwerf LynoHD · Oct 19, 2016 at 12:28 PM 0
Share

@LynoHD, well to be honoust, I'm not an expert on the delegate either.You can see it as a variable where you contain a function in. Then you can call multiple functions from that function(I have not found out why it's handy yet). But I find the button clicks work perfectly with the delegate function in it.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Button OnClick() calling wrong function 1 Answer

My button does not assign any variable value,My OnClick button works with Debug.Log, but doesnt assign any values it should 0 Answers

Upgrade Button 3 & 4 calling on the Upgrade 2 method 1 Answer

Issue with spawning buttons and assigning listeners 1 Answer

OnClick() animation 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