- Home /
Problem starting the game with a key
Hello everyone, I have a problem. I created a script so that my game would start when I pressed the X key and everything was fine, but I don't know what moved it and now it doesn't work, the game simply starts when I press play, I already checked the code and I see it. ok as it is supposed to work if someone can help me i appreciate it
This is my code
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour { public GameObject menuPrincipal; public GameObject menuGameOver;
public float velocidad = 2;
public GameObject col;
public GameObject piedra1;
public GameObject aleteo;
public GameObject aleteo2;
public Renderer fondo;
public bool gameOver = false;
public bool start = false;
public List<GameObject> cols;
public List<GameObject> obstaculos;
public List<GameObject> volador;
// Start is called before the first frame update
void Start()
{
// Crear Mapa
for (int i = 0; i < 21; i++)
{
cols.Add(Instantiate(col, new Vector2(-8 + i, -5), Quaternion.identity));
}
// Crear Piedras
obstaculos.Add(Instantiate(piedra1, new Vector2(-3, -3), Quaternion.identity));
obstaculos.Add(Instantiate(aleteo, new Vector2(5, -3), Quaternion.identity));
volador.Add(Instantiate(aleteo2, new Vector2(14, -1.6f), Quaternion.identity));
}
// Update is called once per frame
void Update()
{
if (start == false)
{
if (Input.GetKeyDown(KeyCode.X))
{
start = true;
}
}
if (start == true && gameOver == true)
{
menuGameOver.SetActive(true);
if (Input.GetKeyDown(KeyCode.X))
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
if (start == true && gameOver == false)
{
menuPrincipal.SetActive(false);
fondo.material.mainTextureOffset = fondo.material.mainTextureOffset + new Vector2(0.05f, 0) * Time.deltaTime;
// Mover Mapa
for (int i = 0; i < cols.Count; i++)
{
if (cols[i].transform.position.x <= -10)
{
cols[i].transform.position = new Vector3(10, -5, 0);
}
cols[i].transform.position = cols[i].transform.position + new Vector3(-1, 0, 0) * Time.deltaTime * velocidad;
}
// Mover Obstaculos
for (int i = 0; i < obstaculos.Count; i++)
{
if (obstaculos[i].transform.position.x <= -10)
{
float randomObs = Random.Range(11, 18);
obstaculos[i].transform.position = new Vector3(randomObs, -3, 0);
}
obstaculos[i].transform.position = obstaculos[i].transform.position + new Vector3(-1, 0, 0) * Time.deltaTime * velocidad;
}
for (int i = 0; i < volador.Count; i++)
{
if (volador[i].transform.position.x <= -10)
{
float randomObs = Random.Range(11, 18);
volador[i].transform.position = new Vector3(randomObs, -1.6f, 0);
}
volador[i].transform.position = volador[i].transform.position + new Vector3(-1, 0, 0) * Time.deltaTime * velocidad;
}
}
}
}
Have you saved the scene or prefab with the start
field set to true?
Answer by Caeser_21 · Mar 24 at 05:19 AM
Make sure you have clicked on your game view (Selected it), as this can cause cause the problem sometimes...
Also try using Input.GetKeyDown("x")
instead of Input.GetKeyDown(KeyCode.X)