Question by
Abdu_Ki · Jul 10, 2017 at 10:01 PM ·
c#if-statementsupdate functionkeypressreloading
Why does this condition keep being true even if its not?
Hello.
So I developed a small game and it runs very well except for the part where I want the game to reload if a condition is true. I want to reload the game scene when two integers don't have the same value but only WHEN THE SPACE KEY IS PRESSED. Somehow the game reloads without the key even being pressed. What am I doing wrong? I hope you guys can help me...
Here is the part that is causing the problem (i guess):
void Update () {
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began || Input.GetKey (KeyCode.Space)) {
if (startText != null) {
Destroy (startText);
Time.timeScale = 1f;
}
if (signal == pointerSignal) {
score += 1;
GetComponent<AudioSource> ().PlayOneShot (coin, 1f);
rotationValue *= -1f;
rotationValue *= 1.05f;
rand = Random.value * 10f;
if (rand < 2.5f) {
pointerChild.GetComponent<SpriteRenderer> ().sprite = Resources.Load<Sprite> ("redp");
pointerSignal = 1;
} else if (rand >= 2.5f && rand < 5f) {
pointerChild.GetComponent<SpriteRenderer> ().sprite = Resources.Load<Sprite> ("greenp");
pointerSignal = 2;
} else if (rand >= 5f && rand < 7.5f) {
pointerChild.GetComponent<SpriteRenderer> ().sprite = Resources.Load<Sprite> ("bluep");
pointerSignal = 3;
} else if (rand >= 7.5f && rand <= 10f) {
pointerChild.GetComponent<SpriteRenderer> ().sprite = Resources.Load<Sprite> ("yellowp");
pointerSignal = 4;
}
}
} else {
SceneManager.LoadScene(SceneManager.GetActiveScene().name.ToString());
}
scoreText.text = "Score: " + score.ToString ();
highscore.text = "High Score: " + PlayerPrefs.GetInt ("highscore", 0).ToString ();
Debug.Log ("Signal: " + signal + " Pointer Signal: " + pointerSignal);
}
Here is the whole (and only) script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class InputController : MonoBehaviour {
private float rotationValue = -1f;
private int signal;
private int pointerSignal = 0;
private float rand;
public GameObject pointerChild;
private int score = 675;
public Text scoreText;
public Text startText;
public Text highscore;
public AudioClip coin;
void Awake(){
Time.timeScale = 0;
rand = Random.value * 10f;
PlayerPrefs.GetInt ("highscore", 0);
}
void Start(){
if (rand < 2.5f) {
pointerChild.GetComponent<SpriteRenderer> ().sprite = Resources.Load<Sprite> ("redp");
pointerSignal = 1;
} else if (rand >= 2.5f && rand < 5f) {
pointerChild.GetComponent<SpriteRenderer> ().sprite = Resources.Load<Sprite> ("greenp");
pointerSignal = 2;
} else if (rand >= 5f && rand < 7.5f) {
pointerChild.GetComponent<SpriteRenderer> ().sprite = Resources.Load<Sprite> ("bluep");
pointerSignal = 3;
} else if (rand >= 7.5f && rand <= 10f) {
pointerChild.GetComponent<SpriteRenderer> ().sprite = Resources.Load<Sprite> ("yellowp");
pointerSignal = 4;
}
}
void Update () {
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began || Input.GetKey (KeyCode.Space)) {
if (startText != null) {
Destroy (startText);
Time.timeScale = 1f;
}
if (signal == pointerSignal) {
score += 1;
GetComponent<AudioSource> ().PlayOneShot (coin, 1f);
rotationValue *= -1f;
rotationValue *= 1.05f;
rand = Random.value * 10f;
if (rand < 2.5f) {
pointerChild.GetComponent<SpriteRenderer> ().sprite = Resources.Load<Sprite> ("redp");
pointerSignal = 1;
} else if (rand >= 2.5f && rand < 5f) {
pointerChild.GetComponent<SpriteRenderer> ().sprite = Resources.Load<Sprite> ("greenp");
pointerSignal = 2;
} else if (rand >= 5f && rand < 7.5f) {
pointerChild.GetComponent<SpriteRenderer> ().sprite = Resources.Load<Sprite> ("bluep");
pointerSignal = 3;
} else if (rand >= 7.5f && rand <= 10f) {
pointerChild.GetComponent<SpriteRenderer> ().sprite = Resources.Load<Sprite> ("yellowp");
pointerSignal = 4;
}
}
} else {
SceneManager.LoadScene(SceneManager.GetActiveScene().name.ToString());
}
scoreText.text = "Score: " + score.ToString ();
highscore.text = "High Score: " + PlayerPrefs.GetInt ("highscore", 0).ToString ();
Debug.Log ("Signal: " + signal + " Pointer Signal: " + pointerSignal);
}
void FixedUpdate(){
transform.Rotate (0, 0, rotationValue);
}
void OnCollisionEnter2D(Collision2D coll){
if (coll.gameObject.tag == "Red") {
signal = 1;
}
if (coll.gameObject.tag == "Green") {
signal = 2;
}
if (coll.gameObject.tag == "Blue") {
signal = 3;
}
if (coll.gameObject.tag == "Yellow") {
signal = 4;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
C# Check for multiple key presses and their combinations 0 Answers
AudioSource in Update 1 Answer
Play audio once while condition is met in update function (C#) 2 Answers
Help with rotation please 2 Answers
Infinite Time inbetween if statements? 2 Answers