Mobile Buttons not working?
Hi,
This is the script attacted to my Sprite Button to Move Left:
#pragma strict
var Button : Sprite;
var Button_Down : Sprite;
var player : GameObject;
function Start ()
{
player = GameObject.Find("player");
}
function OnMouseEnter()
{
GetComponent(SpriteRenderer).sprite = Button_Down;
}
function OnMouseExit()
{
GetComponent(SpriteRenderer).sprite = Button;
}
function OnMouseDown()
{
player.GetComponent(playerScript).MoveLeft();
}
And this my playerScript (I wanted to be able to control with WASD - keycodes, and the buttons, I can control with keys, but the sprite buttons don't do anything..)
#pragma strict
var moveUp : KeyCode;
var moveDown : KeyCode;
var moveLeft : KeyCode;
var moveRight : KeyCode;
var GameManager : GameObject;
var speed : float = 10;
var stepSize : float = 1.6;
function Start ()
{
GameManager = GameObject.Find("_GM");
}
function MoveUp ()
{
transform.Translate(0, stepSize, 0);
GameManager.GetComponent.<gameManagerScript>().currentSteps += 1;
}
function MoveDown ()
{
GameManager.GetComponent.<gameManagerScript>().currentSteps += 1;
transform.Translate(0, -stepSize, 0);
}
function MoveLeft ()
{
GameManager.GetComponent.<gameManagerScript>().currentSteps += 1;
transform.Translate(-stepSize, 0, 0);
}
function MoveRight ()
{
transform.Translate(stepSize, 0, 0);
GameManager.GetComponent.<gameManagerScript>().currentSteps += 1;
}
function Update()
{
// moving up
if(Input.GetKeyDown(moveUp))
{
MoveUp();
}
// moving down
else if(Input.GetKeyDown(moveDown))
{
MoveDown();
}
// moving left
else if(Input.GetKeyDown(moveLeft))
{
MoveLeft();
}
// moving right
else if(Input.GetKeyDown(moveRight))
{
MoveRight();
}
// stand still
else
{
GetComponent.<Rigidbody2D>().velocity.y = 0;
}
}
But the button IS not working. Yes I have added an 2D box collider with 'Is Trigger' enabled.
Any help would be appreciated!
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612050502im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Programmatically creating buttons with images that can be one of dozens. 0 Answers
UI Sprite not changing when button is pressed 1 Answer
Removing If-statements 2 Answers
Glowing 2D items 0 Answers