Can't find error in code
I have a piece of code that seems to not be working properly and I can't seem to figure out why... Basically, I have a nuke button that "kills" all enemies currently spawned, and this is activated by pressing a UI button. The nuke function itself works fine, however after using the nuke button once in a game, another nuke is used every time the space bar is pressed on the keyboard...
Here is the code for the nuke button:
public void NukePowerup(){
if (nukeCount > 0 && !gameover) {
nukeCount--;
PlayerPrefs.SetInt ("nukeCount", nukeCount);
enemies.AddRange (GameObject.FindGameObjectsWithTag ("Enemy1"));
int one = enemies.Count;
enemies.AddRange (GameObject.FindGameObjectsWithTag ("Enemy2"));
int two = enemies.Count - one;
enemies.AddRange (GameObject.FindGameObjectsWithTag ("Enemy3"));
int three = enemies.Count - two - one;
enemies.AddRange (GameObject.FindGameObjectsWithTag ("Enemy4"));
int four = enemies.Count - three - two - one;
scoreMultiplier += enemies.Count;
int gains;
if (nukeLvl == 1) {
gains = ((one * 100 * scoreMultiplier) + (two * 130 * scoreMultiplier) + (three * 170 * scoreMultiplier) + (four * 300 * scoreMultiplier));
if (charNumber == 3) {
gains = (int)(gains * 1.2f);
}
} else if (nukeLvl == 2) {
gains = ((one * 150 * scoreMultiplier) + (two * 195 * scoreMultiplier) + (three * 255 * scoreMultiplier) + (four * 450 * scoreMultiplier));
if (charNumber == 3) {
gains = (int)(gains * 1.2f);
}
} else if (nukeLvl == 3) {
gains = ((one * 200 * scoreMultiplier) + (two * 260 * scoreMultiplier) + (three * 340 * scoreMultiplier) + (four * 600 * scoreMultiplier));
if (charNumber == 3) {
gains = (int)(gains * 1.2f);
}
} else {
gains = ((one * 100 * scoreMultiplier) + (two * 130 * scoreMultiplier) + (three * 170 * scoreMultiplier) + (four * 300 * scoreMultiplier));
if (charNumber == 3) {
gains = (int)(gains * 1.2f);
}
}
score += gains;
if (scoreMultiplier <= 1) {
Scorepopup.text = "+ " + gains.ToString ();
} else {
Scorepopup.text = scoreMultiplier.ToString () + " Chain \n" + "+ " + gains.ToString();
}
Text scorepop = Instantiate (Scorepopup) as Text;
scorepop.transform.SetParent (GameObject.Find ("Canvas").transform);
scorepop.transform.localPosition = transform.position;
foreach (GameObject enemy in enemies) {
if (enemy != null) {
enemy.GetComponent<Rigidbody> ().useGravity = true;
enemy.tag = "dead";
}
}
scoreMultiplier = 0;
usedNuke = true;
checkChallenges ();
powerupsUsed++;
PlayerPrefs.SetInt ("powerupsUsed", powerupsUsed);
}
}
The only code I have relating to the pressing of the space button in the script is as follows:
void FixedUpdate(){
isGrounded = Physics.Raycast (transform.position, Vector3.down, 0.565f, groundLayer);
if (Input.GetAxis ("Jump") != 0f && isGrounded) {
if (!killBox.GetComponent<AudioSource> ().isPlaying) {
killBox.GetComponent<AudioSource> ().Play ();
}
GetComponent<Rigidbody> ().AddForce (new Vector3 (0, Input.GetAxis ("Jump") * jumpPower, 0), ForceMode.Impulse);
}
}
Any help would be greatly appreciated, as I have absolutely no clue what is causing this...
Answer by Positive7 · Feb 22, 2018 at 10:27 PM
UI.Button -> Navigation -> Set to None
https://docs.unity3d.com/Manual/script-SelectableNavigation.html
That is interesting. It shouldn't respond to any key press.
Yeah, it is. I have no clue what could be causing it
Your answer
Follow this Question
Related Questions
Level Start error :/ 1 Answer
NetworkClient Send with no Connection 0 Answers
There's an issue with this script I can't find 0 Answers
NullReferenceException: Object reference not set to an instance of an object Problem! 0 Answers
Invoke Repeating Problem 0 Answers