- Home /
Onscreen button (touch) trigger event doesn't trigger properly on build
Hi! I've been trying to debug this for 2 days now: I have an onscreen button which uses Unity's Event Trigger to call (Pointer Down) the player's jump function [OnJump()] and a device vibration [vibrateButton()] and then it also triggers a boolean inside the code. Here's the problem: it works fine in the editor, but in build it seems to only trigger the Jump function (vibration and boolean are not affected). I've used an on-build debugger console and got the following error when I try to trigger the button:
NullReferenceException: Object reference not set to an instance of an object Vibration.Vibrate (System.Int64 milliseconds) (at :0) Button_Vibrate.vibrateButton () (at :0) UnityEngine.Events.InvokableCall.Invoke () (at :0) UnityEngine.Events.UnityEvent`1[T0].Invoke (T0 arg0) (at :0) UnityEngine.EventSystems.EventTrigger.Execute (UnityEngine.EventSystems.EventTriggerType id, UnityEngine.EventSystems.BaseEventData eventData) (at :0) UnityEngine.EventSystems.EventTrigger.OnPointerDown (UnityEngine.EventSystems.PointerEventData eventData) (at :0) UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerDownHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at :0) UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at :0) UnityEngine.EventSystems.EventSystem:Update()
I've been trying to track down the source of the problem, researched a lot on forums, tested and re-tested with many changes on builds, but can't seem to be able to fix it.
Here's all the code involved in the button trigger:
Setting the action in Awake for the boolean to trigger:
void Awake()
{
jumpButton = GameObject.Find(jumpButtonName);
EventTrigger jumpTrigger = jumpButton.GetComponent<EventTrigger>();
EventTrigger.Entry jumpEntryDown = new EventTrigger.Entry();
jumpEntryDown.eventID = EventTriggerType.PointerDown;
jumpEntryDown.callback.AddListener((data) => { OnPointerDownDelegate((PointerEventData)data); });
jumpTrigger.triggers.Add(jumpEntryDown);
EventTrigger.Entry jumpEntryUp = new EventTrigger.Entry();
jumpEntryUp.eventID = EventTriggerType.PointerUp;
jumpEntryUp.callback.AddListener((data) => { OnPointerUpDelegate((PointerEventData)data); });
jumpTrigger.triggers.Add(jumpEntryUp);
}
Triggering the boolean when the button is pressed/released (does not trigger on build):
public void OnPointerDownDelegate(PointerEventData data)
{
jumpIsHeld = true;
}
public void OnPointerUpDelegate(PointerEventData data)
{
jumpIsHeld = false;
}
The jump function (which triggers fine on both the editor and build):
public void OnJump()
{
if (hangCounter > 0f && !isJumping) //If the player is on the ground;
{
playerRB.velocity = Vector2.zero;
playerRB.AddForce(new Vector2(playerRB.velocity.x, jumpStrength));
isJumping = true;
}
}
And the the vibration script which also does not trigger on build:
using UnityEngine;
using System.Collections;
public class Button_Vibrate : MonoBehaviour
{
public long vibrationDuration = 30;
public void vibrateButton()
{
Vibration.Vibrate(vibrationDuration);
}
}
It is referencing a static class from another script ("Vibration").
So, yeah. When the button is pressed, the Jump() function is triggered normally in build, but the boolean jumpIsHeld and the function vibrateButton() are not. As I said, they all behave as intended in the editor.
I've been trying to figure this out for a while now and have come across some people with similar issues but I haven't come to a solution...
Does anyone know what might be happening?
Hmm, i actually had the same problem, what fixed it for me was searchig for using UnityEditor
or Debug.Log $$anonymous$$ethod,
Hey thanks for the response! Could elaborate a little more on how exactly you solved it? I'm not sure I understand it. The debug logs work fine for me in the editor, but then again not on build :\
Oh if forgot a part of the sentence :D I meant I removed them because you're doing actions in the Unity Editor, but when you have a build version the Unity Editor isn't there anymore, so it would resolve in an error (I think so). Example: You try giving a Debug.Log in the Unity Console, which works, but in a seperate programm ther is no unity console, so this cant work.
Confirmation: the Input System has nothing to do with it