I ve read many topics but i cannot find the answer to my problem.I have this code tha detects touch and the second tha checks if the boolean is true and executes some code.But it doesnt work
LEFT BUTTON SCRIPT
using UnityEngine;
using System.Collections;
public class LeftButton : MonoBehaviour
{
public bool moveLeft;
void Update ()
{
//is there a touch on screen?
if(Input.touches.Length <= 0)
{
//if no touches then execute this code
}
else //if there is a touch
{
//loop through all the the touches on screen
for(int i = 0; i < Input.touchCount; i++)
{
//executes this code for current touch (i) on screen
if(this.guiTexture != null (this.guiTexture.HitTest(Input.GetTouch(i).position)))
{
//if current touch hits our guitexture, run this code
if (Input.GetTouch(i).phase == TouchPhase.Stationary)
{
moveLeft = true;
}
if (Input.GetTouch(i).phase == TouchPhase.Ended)
{
moveLeft = false;
}
}
}
}
}
}
MOVEMENT CONTROLLER SCRIPT
using UnityEngine;
using System.Collections;
public class MovementController : MonoBehaviour
{
public float maxSpeed = 3.0f;
GameObject leftArrow;
GameObject rightArrow;
LeftButton scriptLeft;
RightButton scriptRight;
void Start ()
{
leftArrow = GameObject.Find ("Left Arrow");
rightArrow = GameObject.Find ("Right Arrow");
scriptLeft = rightArrow.GetComponent<LeftButton>();
scriptRight = leftArrow.GetComponent<RightButton>();
}
void Update ()
{
if (scriptRight.moveRight == true)
{
anim.SetBool("moving",true);
rigidbody2D.velocity = new Vector2 (maxSpeed, rigidbody2D.velocity.y);
}
if (scriptLeft.moveLeft == true)
{
anim.SetBool("moving",true);
rigidbody2D.velocity = new Vector2 (-maxSpeed, rigidbody2D.velocity.y);
}
I’ tried also
if (scriptRight.moveRight)
{
anim.SetBool("moving",true);
rigidbody2D.velocity = new Vector2 (maxSpeed, rigidbody2D.velocity.y);
}
if (!scriptLeft.moveLeft)
{
anim.SetBool("moving",true);
rigidbody2D.velocity = new Vector2 (-maxSpeed, rigidbody2D.velocity.y);
}