Check an if statement before every button action

I’m making a game and I want the script to work on both PC and mobile devices. One problem I’m having with mobile devices is having to check if more then one button is being pressed. I know you can check this by using Input.touchCount. I expect for my game I will use something like this…

// Set true if the game is on a mobile device.
bool Mobile 

If (Mobile == false || Input.touchCount == 1)
{
// Do Click Action
}

But how could I add this to every click without having to constantly rewrite it. I mean this game is going to have a LOT of button commands.

Perhaps you could create a game controller with a script for handling touches? And put your if statement within a public bool function in that and only allow the click action if that function returns true. I don’t quite understand what you mean by checking if more than one button is being pressed. Could you explain the context perhaps?

An example:

//TouchHandler.cs

public bool IsValid()
{
 //do your checks here and return true/false depending on if the touch is valid
}

//A script for controlling what happens with the button
void Start()
{
gameController = GameObject.FindGameObjectWithTag("GameController");
}

public void DoSomething()
{
if (gameController.GetComponent<TouchHandler>().IsValid())
{
//do click action
}
}

Hope this helps somewhat

I want to make my game work on a mobile device, But I can’t have people touching two on screen buttons at once or it would mess up the game. So before doing the action I need to check the touch count, but my game has a LOT of on screen buttons, and I don’t want to have to write an if statement for all of them, so I wanted to know a shortcut.

I pretty much want a shortcut so I don’t have to write this for all my buttons.

public void ButtonAction()
{
If (Input.touchCount == 1)
 {
 // Do Click Action
 }
}

You’re about to launch into a new phase of object oriented programming. You’ve discovered a pattern that must be repeated and you want to wrap that concept into something that does it intelligently, while making the rest of your code cleaner and simpler.
That’s what objects are for!
Objects, in case you’re not sure, are the instantiations of a class.
You’ve outlined exactly what hints to the existence of an object that has not yet been written in your code. Learn to notice this situation.
Consider, now, what owns “Input” in your example code. I can’t see what that is. You are, at present, required to repeat code using “Input” to determine what to do.

Now, create a stand alone class and move “Input” into that class, and remove it from the class that now owns it (I can’t offer specifics because I can’t see the related code from your project). You choose a name you prefer, but for now I’ll call the class “SmartInput”, which owns the “Input” you’re constantly having to test, repeatedly.

At first, this would create havoc in the compiler, but that’s good. Every complaint now shows where the change impacts what you’ve written thus far.

Add a member variable to the class that once owned “Input”, but the type is now SmartInput. It replaces Input (absorbs it, if you like). Don’t forget you have to “new” it, something like:

private SmartInput sInput = new SmartInput();

You may have to include more information in the new SmartInput class. Since I can’t see your code, I can’t be certain what all that will be. You’ll discover while you attempt to complete this work.

To be clear, whatever your original code did to initialize an input must now be moved to the SmartInput class. This is where you may discover some member variables to add to SmartInput. You may even decide to create a constructor for SmartInput that accepts initializing values. The bool Mobile may be one of these.

Now, consider what you do now with your example like:

 If (Mobile == false || Input.touchCount == 1)
 {
 // Do Click Action
 }

This will move into SmartInput. Now consider, if you didn’t have to deal with “Mobile” or with “touchCount”, what would you be doing to sense that Input has been touched? Probably something that is a simple bool test, like “IsTouched”, without regard to the touch count or the state of Mobile.

Fashion a function in SmartInput for this. I’d say “IsTouched()” is ok, something like:

public bool IsTouched()
{
 if ( Mobile == false || Input.touchCount == 1 ) return true;
 return false;
}

Now, there’s only one of these, in the SmartInput class.

Everywhere you’ve had to do this in the “old class” you’ve been using, you can merely do:

if ( sInput.IsTouched() == true ) 
   {
    // do what touch should do
   }

Now, the task of deciding how a touch is interpreted is in the SmartInput class. You can have several of them anywhere you need them. It can be reused in future projects.

You’ve fashioned a tool.

That’s what objects do. They create small logical collections of thought, wrapped in the class, which then operate like components fitted together in a real machine.

When you practice this, you’ll start to see objects everywhere. When you notice you’re repeating something, it probably hints an object should be fashioned to do that job.

With all of that said, there is an alternative when the subject the proposed class is doing is so small it seems too much to create a class for it. You can fall back to an older idea that comes from C, Pascal and all the languages that have functions.

So, the other method, which does not involve changing Input or a creating a new class. Instead, you just fashion a function.

Instead of directly using Input, everywhere you now check Input for touchcount (and Mobile for true), call a function “bool IsTouched()”, in the same class that now owns Input. Stop using Input everywhere, and replace those calls to the “IsTouched” function, where now you have one place that checks the state of Mobile and the touchCount, returning true or false from one piece of code.

Both are valid solutions. I detail the creation of a class because this is a classic opportunity to realize what objects can do to solve problems like this, though usually they solve larger problems than you describe with your use of “Input”.