Search Unity

Bug How can I get touch input in my game?

Discussion in 'Scripting' started by TheDirtDiver, Mar 28, 2023.

  1. TheDirtDiver

    TheDirtDiver

    Joined:
    Aug 18, 2021
    Posts:
    2
    I just need to detect a input of touch (Anywhere not just a certain part) for my game. I feel like im doing this right its probally just the way that I wrote this if statement.

    Code (CSharp):
    1.         if (Input.GetKeyDown(KeyCode.Space) && (TouchingGoal == true) && (GameStarted == true) || (Input.GetTouch) && (TouchingGoal == true) && (GameStarted == true))
    2.         {
    3.             Score = Score + 1;
    4.             noise2.Play();
    5.         }
    I get an error and it says I cant use && after the Input.GetTouch

    "Operator '&&' cannot be applied to operands of type 'method group' and 'bool'"

    Also I am kind of new to unity so be nice lol!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,688
    Rather than ram it all into one line, try this approach:

    - make some temporary variables to store possible input

    - read input from keyboard into the variables

    - read it from touch into the variables

    - ready it from somewhere else too if you like

    - now act on those variables


    If you have more than one or two dots (.) in a single statement, you're just being mean to yourself.

    How to break down hairy lines of code:

    http://plbm.com/?p=248

    Break it up, practice social distancing in your code, one thing per line please.

    "Programming is hard enough without making it harder for ourselves." - angrypenguin on Unity3D forums

    "Combining a bunch of stuff into one line always feels satisfying, but it's always a PITA to debug." - StarManta on the Unity3D forums
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,454
    Please know that this isn't a 2D related post so please try to use the dedicated forums provided. In this case, I would recommend using the Scripting or Getting Started forums.

    I'll move your post for you.

    Thanks.
     
  4. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    its because you cant use input.gettouch that way, you should do "Input.touchCount > 0" if you just want to see if the screen is getting touched
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,688