Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question New Input System releasing hold touch wont work

Discussion in 'Input System' started by unity_6103548EB384C2C58CE0, Sep 13, 2023.

  1. unity_6103548EB384C2C58CE0


    Feb 3, 2023
    Noob question. Hi, i want to ask why my OnHoldCanceled is not working when i dont touch the screen anymore.

    This is my code

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.InputSystem;

    public class TowerPlacement : MonoBehaviour
    PlayerInput playerInput;

    // private InputAction _touchPressedAction;
    private InputAction _touchHoldAction;

    Ray _position;
    bool _isPlacing;

    [SerializeField] LayerMask layer;
    [SerializeField] private GameObject _tower;
    [SerializeField] private Camera _mainCam;

    // [SerializeField] private Material mainMat;
    // [SerializeField] private Material transpMat;

    [SerializeField] Material[] _materia;
    Renderer rend;

    // Material mat;

    GameObject _instObj;

    private void Awake()
    playerInput = GetComponent<PlayerInput>();
    _touchHoldAction = playerInput.actions["Hold"];
    _touchHoldAction.performed += OnPlacing;
    _touchHoldAction.canceled += OnHoldCanceled;


    private void OnDisable()

    _touchHoldAction.performed -= OnPlacing;
    _touchHoldAction.canceled -= OnHoldCanceled;


    public void OnPressed()
    if (!_isPlacing)
    _instObj = Instantiate(_tower, transform.position, Quaternion.identity);
    rend = _instObj.GetComponent<Renderer>();
    rend.sharedMaterial = _materia[1];
    _isPlacing = true;

    public void OnHoldCanceled(InputAction.CallbackContext context)
    if (_isPlacing)

    _isPlacing = false;
    rend.sharedMaterial = _materia[0];
    Debug.Log("Touch Ended");

    public void OnPlacing(InputAction.CallbackContext context)
    _position = _mainCam.ScreenPointToRay(Touchscreen.current.position.ReadValue());

    if (Physics.Raycast(_position, out RaycastHit raycastHit) && _isPlacing)
    _instObj.transform.position = raycastHit.transform.position;
    Debug.DrawRay(_position.origin, _position.direction * 20,;
    Debug.Log("Touch Started");

    this script is in a button and the OnPressed function is on the onclick event. My aim is to click the button to select a tower to place. then use the _touchHoldAction to track where my finger is... but it the canceled event wont work... sorry my english is not very good and still a noobie. sorry for the noob question.