Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Invalid Touch Index

Discussion in 'Input System' started by Kindacringegamemaker, Aug 25, 2023.

  1. Kindacringegamemaker

    Kindacringegamemaker

    Joined:
    Jul 8, 2023
    Posts:
    14
    (Argument Exception: Invalid Touch Index) this error keeps showing up and I'm getting really annoyed with it, the touch works but it's the errors that keep flooding in every millisecond, here's the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using DG.Tweening;
    5.  
    6. public class PlaneControls : MonoBehaviour
    7. {
    8.  
    9.     [SerializeField] Rigidbody2D rb2D;
    10.     [SerializeField] int flyspeed;
    11.     [SerializeField] int tweenlimit;
    12.     [SerializeField] int  sequence;
    13.  
    14.  
    15.     // Start is called before the first frame update
    16.  
    17.     // Update is called once per frame
    18.     void FixedUpdate()
    19.     {
    20.         Touch touch = Input.GetTouch(0);
    21.        
    22.        
    23.         if (touch.phase == TouchPhase.Stationary)
    24.         {
    25.            
    26.            
    27.             rb2D.velocity = Vector2.up * flyspeed;
    28.            
    29.            
    30.  
    31.  
    32.         }
    33.        
    34.     }
    35. }
    36.  
    The only reason why I put the Touch in FixedUpdate() is because when I put it in Awake() or Start() it won't detect any touches and still pushes out that same error, I really need some help please.
     
  2. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    488
    Perhaps you need to check if there is actually a touch happening by using the touchCount. Without that it might be trying to get a touch when nothing is happening.

    Maybe do this:
    Code (csharp):
    1.    void FixedUpdate()
    2.     {
    3.        if (Input.touchCount > 0)
    4.        {
    5.                Touch touch = Input.GetTouch(0);      
    6.                if (touch.phase == TouchPhase.Stationary)
    7.                {          
    8.                               rb2D.velocity = Vector2.up * flyspeed;          
    9.                }
    10.        }
    11.     }
    12.  
     
    viknesh2020 likes this.
  3. Kindacringegamemaker

    Kindacringegamemaker

    Joined:
    Jul 8, 2023
    Posts:
    14
    Thanks that worked
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,511
    Please use the Input system or Scripting sub-forums, not the 2D forum for questions like this.

    I'll move your post for you.

    Thanks.