Counter not working properly
Hello I am new to unity and I am trying to make a counter that adds +1 when i press right, -1 when i press left to one counter called "contadorx" and +1 when I press up, - 1 when I press down to a counter called "contadory". My problem is that when I press the right key it displays the value of 1 but never goes beyond that, so it's not adding 1, thus not working properly. Please excuse my code this is my first time using unity and I am just messing around with an idea I had. Thanks! using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Move : MonoBehaviour
{
public GameObject playerprefab;
int contadorx ;
int contadory ;
private void Start()
{
contadorx = 0;
contadory = 0;
}
void Update()
{
if (Input.GetKey("right") && (contadorx <= 29) && !(Input.GetKey("left") && Input.GetKey("up") && Input.GetKey("down")))
{
MoveRight();
}
if (Input.GetKey("left") && (contadorx >= 1) && !(Input.GetKey("right") && Input.GetKey("up") && Input.GetKey("down")))
{
MoveLeft();
}
if (Input.GetKey("up") && (contadory <= 9) && !(Input.GetKey("right") && Input.GetKey("left") && Input.GetKey("down")))
{
MoveUp();
}
if (Input.GetKey("down") && (contadory >= 1) && !(Input.GetKey("right") && Input.GetKey("left") && Input.GetKey("up")))
{
MoveDown();
}
}
void MoveRight()
{
Instantiate(playerprefab, new Vector3(transform.position.x + 1, transform.position.y, transform.position.z), transform.rotation );
Destroy(gameObject);
contadorx = contadorx + 1;
print(contadorx);
}
void MoveLeft()
{
Instantiate(playerprefab, new Vector3(transform.position.x - 1, transform.position.y, transform.position.z), transform.rotation);
Destroy(gameObject);
contadorx = contadorx - 1;
}
void MoveUp()
{
Instantiate(playerprefab, new Vector3(transform.position.x, transform.position.y, transform.position.z + 1), transform.rotation);
Destroy(gameObject);
contadory = contadory + 1;
}
void MoveDown()
{
Instantiate(playerprefab, new Vector3(transform.position.x, transform.position.y, transform.position.z - 1), transform.rotation);
Destroy(gameObject);
contadory = contadory - 1;
}
}
,Hello I made a counter that should add +1 when moving to the right and -1 when moving to the left to one counter and add + 1 when moving up and -1 when moving down to another counter. My problem is I am only getting the value of "contadorx" to be 1, every time I press the right key the console displays the value of 1 so it's not adding one each time.Please excuse the code, this is my first time with unity and I was just messing around with an idea I had. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Move : MonoBehaviour
{
public GameObject playerprefab;
int contadorx ;
int contadory ;
private void Start()
{
contadorx = 0;
contadory = 0;
}
void Update()
{
if (Input.GetKey("right") && (contadorx <= 29) && !(Input.GetKey("left") && Input.GetKey("up") && Input.GetKey("down")))
{
MoveRight();
}
if (Input.GetKey("left") && (contadorx >= 1) && !(Input.GetKey("right") && Input.GetKey("up") && Input.GetKey("down")))
{
MoveLeft();
}
if (Input.GetKey("up") && (contadory <= 9) && !(Input.GetKey("right") && Input.GetKey("left") && Input.GetKey("down")))
{
MoveUp();
}
if (Input.GetKey("down") && (contadory >= 1) && !(Input.GetKey("right") && Input.GetKey("left") && Input.GetKey("up")))
{
MoveDown();
}
}
void MoveRight()
{
Instantiate(playerprefab, new Vector3(transform.position.x + 1, transform.position.y, transform.position.z), transform.rotation );
Destroy(gameObject);
contadorx = contadorx + 1;
print(contadorx);
}
void MoveLeft()
{
Instantiate(playerprefab, new Vector3(transform.position.x - 1, transform.position.y, transform.position.z), transform.rotation);
Destroy(gameObject);
contadorx = contadorx - 1;
}
void MoveUp()
{
Instantiate(playerprefab, new Vector3(transform.position.x, transform.position.y, transform.position.z + 1), transform.rotation);
Destroy(gameObject);
contadory = contadory + 1;
}
void MoveDown()
{
Instantiate(playerprefab, new Vector3(transform.position.x, transform.position.y, transform.position.z - 1), transform.rotation);
Destroy(gameObject);
contadory = contadory - 1;
}
}
Sorry about it being repeated, but as I was not logged in when I tried to post it the first time I had to rewrite everything and it seems for some reason it duplicated