Please help. Attempting touch controls but I cant figure out what I'm doing wrong

Hi.
I am trying to make a game for smartphones and have found several touch screen tutorials online such as this one Unity Touch Input Buttons Tutorial (Part 1) - YouTube which is the one I have been attempting to emulate. I have some code but cannot understand why it isn’t working.

I have managed to get Unity and the Android SDK working in tandem so I can install the app on my phone. Although it loads fine nothing happens when I touch the GUI texture/s. Any help is greatly appreciated. Here is my code.

using UnityEngine;
using System.Collections;

public class __upArrow : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
	for (int i = 0; i < Input.touchCount; i++) {
		if (this.guiTexture.HitTest (Input.GetTouch (i).position)) {
			if(Input.GetTouch(i).phase == TouchPhase.Began){
				Player2.Player.Translate (Vector3.up * Player2.playerSpeed * Time.deltaTime);
				}
			}
		}
	}
}

If you get any errors while running your code, you need to find and fix those first. When a script gets an error, it stops processing the rest of the code in the function. It means you may not be running other code unrelated potentially to the error.

I would recommend moving away from using the touch phases and touch input stuff manually unless you have a specific need. Unity maps the OnMouseDown, OnMouseUp, and OnMouseDrag (maybe) methods to the touch methods for you, saving you a lot of pain for simple things like clicks of buttons.

In other words, if you put an OnMouseDown() method in a script on your down arrow button, if you touch the button on the device, that method will be called. In your case, you don’t care if the mouse (finger) is being dragged, but you might be able to use OnMouseDrag() for that as well (I can’t say 100%, haven’t tried it, but it stands to reason it is the same as the others).

So, in OnMouseDown, set a flag indicating the object is down. In OnMouseUp, change the flag to indicate it is not down. In Update(), check if the flag is down, and move the cube accordingly (is one solution).

If you want support for multi touch, i.e. to detect when two arrows are pressed by two fingers, then you’ll likely need to use the Touch Input classes, I can’t say for sure.

I’m not sure exactly why the error is happening but it’s probably happening because of your way of accessing Player2’s variables by making them static. This is not really proper functionality. Really Player2 should all have non-static variables that are public. You can then access them the proper way in your upArrow script as seen below:

// directly set this in the Editor by dragging your Player2 GameObject in the scene here
public GameObject player2GameObject;

private Player2 player2;

void Start () {
    // if not set directly in the Editor then find it by GameObject name which is not as efficient
    player2GameObject = FindByName("Player2");
    player2 = player2GameObject.GetComponent<Player2>();
}

And then continue to reference the Player2 variables as player2.variable instead of Player2.variable.

ITS FINALLY WORKING THANK YOU SO MUCH LEUTHIL!!!

using UnityEngine; using System.Collections;

public class __upArrow : MonoBehaviour {
public GameObject player2Gameobject;
private Player2 player2;

// Use this for initialization
void Start () {
	/*player2GameObject = FindByName("Player2");
	player2 = player2GameObject.GetComponent<Player2>();*/
	
}

// Update is called once per frame
void Update () {
	for (int i = 0; i < Input.touchCount; i++) {
		if (this.guiTexture.HitTest (Input.GetTouch (i).position)) {
			if(Input.GetTouch(i).phase == TouchPhase.Began){
				player2Gameobject.transform.Translate (Vector3.up * 5.0f * Time.deltaTime);
			}
		}
	}
}

}

I had to change the player2 reference to be the player2Gameobject being used from the editor.

If I ever meet you in real life, I owe you!! THANK YOU :slight_smile: