C
C#kitty :3

❔ Code is completely skipping over an area

I'm writing a script for a game where if you don't press Spacebar within 4 seconds, the number of failures increase by 1, the sprite is changes, and the timer resets. This is supposed to happen twice if the button is missed, and if it happens a 3rd time, I want the GameOverScene to appear. What's happening is it directly goes to the GameOverScene when I miss the button and I'm unsure why it's missing the whole sprite change and reset timer aspect
K
kitty :3415d ago
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Random = UnityEngine.Random;
using UnityEngine.SceneManagement;
using TMPro;

//const int POPUP_COOLDOWN = 4;

public class Timer : MonoBehaviour

{
bool can_show_popup = true;
float timer = 0.0f;
bool do_timer = false;
public AudioSource playSound;
public string GameOverScene = "GameOverScene";
public Sprite[] sprites; // An array of sprites to change to if the button is not pressed in time
private bool promptActive = false;
public TextMeshProUGUI promptText; // The TextMeshProUGUI object to display the prompt
private int numFailures = 2; // The number of times the button has not been pressed in time
private SpriteRenderer spriteRenderer;

public void Start()
{
spriteRenderer = GetComponent<SpriteRenderer>(); // Get the sprite renderer component
}


void reset_timer()
{
do_timer = false;
timer = 0.0f;
}

IEnumerator do_popup_debounce()
{
can_show_popup = false;

yield return new WaitForSeconds(4);


can_show_popup = true;
}

void Update()
{

if (can_show_popup)
{
int coin_flip = Random.Range(0, 2);

if (coin_flip == 1)
{
//start timer
do_timer = true;
//show popup

ShowPrompt();

StartCoroutine(do_popup_debounce());

}
}

if (do_timer)
{
timer += 1 * Time.deltaTime;

if (Input.GetKey(KeyCode.Space) && timer < 3.9f)
{
//hide popup
Debug.Log("Space hit");
HidePrompt();
reset_timer();

}

else if (Input.GetKey(KeyCode.Space) == false && timer >= 4f)
{
Debug.Log("Recognized button not hit");
numFailures++;

if (numFailures < 2)
{
Debug.Log("Missed one, boat broke");
spriteRenderer.sprite = sprites[numFailures]; // Change the sprite to the corresponding sprite in the array
HidePrompt();
reset_timer();
playSound.Play();
}
else if (numFailures >= 3)
{
SceneManager.LoadScene(GameOverScene); // Load the game over scene
}
}


}

}
void ShowPrompt()
{
promptActive = true;
promptText.gameObject.SetActive(true); // Activate the prompt text object
}

void HidePrompt()
{
promptActive = false;
promptText.gameObject.SetActive(false); // Deactivate the prompt text object
}

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Random = UnityEngine.Random;
using UnityEngine.SceneManagement;
using TMPro;

//const int POPUP_COOLDOWN = 4;

public class Timer : MonoBehaviour

{
bool can_show_popup = true;
float timer = 0.0f;
bool do_timer = false;
public AudioSource playSound;
public string GameOverScene = "GameOverScene";
public Sprite[] sprites; // An array of sprites to change to if the button is not pressed in time
private bool promptActive = false;
public TextMeshProUGUI promptText; // The TextMeshProUGUI object to display the prompt
private int numFailures = 2; // The number of times the button has not been pressed in time
private SpriteRenderer spriteRenderer;

public void Start()
{
spriteRenderer = GetComponent<SpriteRenderer>(); // Get the sprite renderer component
}


void reset_timer()
{
do_timer = false;
timer = 0.0f;
}

IEnumerator do_popup_debounce()
{
can_show_popup = false;

yield return new WaitForSeconds(4);


can_show_popup = true;
}

void Update()
{

if (can_show_popup)
{
int coin_flip = Random.Range(0, 2);

if (coin_flip == 1)
{
//start timer
do_timer = true;
//show popup

ShowPrompt();

StartCoroutine(do_popup_debounce());

}
}

if (do_timer)
{
timer += 1 * Time.deltaTime;

if (Input.GetKey(KeyCode.Space) && timer < 3.9f)
{
//hide popup
Debug.Log("Space hit");
HidePrompt();
reset_timer();

}

else if (Input.GetKey(KeyCode.Space) == false && timer >= 4f)
{
Debug.Log("Recognized button not hit");
numFailures++;

if (numFailures < 2)
{
Debug.Log("Missed one, boat broke");
spriteRenderer.sprite = sprites[numFailures]; // Change the sprite to the corresponding sprite in the array
HidePrompt();
reset_timer();
playSound.Play();
}
else if (numFailures >= 3)
{
SceneManager.LoadScene(GameOverScene); // Load the game over scene
}
}


}

}
void ShowPrompt()
{
promptActive = true;
promptText.gameObject.SetActive(true); // Activate the prompt text object
}

void HidePrompt()
{
promptActive = false;
promptText.gameObject.SetActive(false); // Deactivate the prompt text object
}

}
i'm really new to this nvm i think i figured it out ^_^ just sprites aren't changing grrrr
K
kakanics415d ago
you are using the Input.GetKey function, use the Input.GetKeyDown function instead.. GetKey will return true whenever the key is being pressed while GetKeyDown will return true only once (when you press the key)
A
Accord414d ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ Registering an extracted appx package```csharp public static void RegisterMinecraftAppxPackage(string appxPackagePath) { try { ✅ Exception Handling for NRE without modifying every call.I have a ReadData function which reads the memory of a process. If the process exits or crashes duri✅ please help idk what to do or what i didSeverity Code Description Project File Line Suppression State Error Project❔ Better user experience inputting commandsHow would I get input from a user, the fancy way? What I mean is that for example when a user types ❔ Return in the middle of a methodIs it ok? I think that it improves readability and performance, but a professor I know argues that i❔ Can't build a MSIX Package for a WPF app❔ Should photo urls be sent paginated to the frontend?So, any given property has a collection of photos in a photo album... they are saved as urls in the ❔ c# wpf textbox access violationHello does anyone know how can i edit the TextBox1.Text within the Task.run so i don't have an acces❔ Help with choosing between ArrayList and other types of collectionsI was challenged to make a pokémon styled game without using classes and I wanted to know better way❔ How can you create a string that creates random characters?Title ^ (Console application)❔ ✅ abstract-Parent and Child static overrides?How do I redeclare an objects element which needs be from a static context in a child class? I have❔ Asking if someone wants to be part of my project (Valorant Discord Rich Presence)Heyheyhey Does some people here play Valorant and want to help me create a Discord Rich Presence usi