- Home /
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!
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.
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();
});
}
Actually
button.onClick.AddListener(() => Rotate());
At least I used it like that.
Yeah that probadly works too, lol. I just always use brackets
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.
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");
}
@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.
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.
@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.
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!
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.
what exactly does the delegate do? I havent gotten into delegates yet
@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.