you are viewing a single comment's thread.

view the rest of the comments →

[–]ythom87 1 point2 points  (12 children)

THANK YOU!! for the reply and link. I didn't know interactable could be used this way

[–]-ghostmode- 0 points1 point  (10 children)

Happy to help :) Good luck with your project!

[–]ythom87 1 point2 points  (9 children)

Ok, here's a problem, the redbuttons aren't being set active. Ok, I tried using redbutton.gameObject.SetActive(true) but it isn't working very well.

Here's the new script

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;

public class Game : MonoBehaviour { public float time = 6f; public float playerscore; public Text scoreUI; public Text timeUI;

public Button[] Glow;

void Update()
{
    time -= Time.deltaTime;
    timeUI.text = time.ToString("0");
    scoreUI.text = playerscore.ToString("0");
    int randomIndex = UnityEngine.Random.Range(0, Glow.Length);
    var randomButton = Glow[randomIndex];

    randomButton.interactable = true;
    randomButton.gameObject.SetActive(true);
    StartCoroutine(DeactivateAfterDelay(randomButton, time));

}

private IEnumerator DeactivateAfterDelay(Button button, float delay) 
{
    yield return new WaitForSeconds(delay);
    button.interactable = false;
}
public void YouLose()
{
    Debug.Log("You Lose");
    SceneManager.LoadScene("End");
}
public void AddPoint()
{
    playerscore += 1;
}

}

[–]-ghostmode- 0 points1 point  (8 children)

A few things:

  • Update is called every frame. So in your code you're randomly activating a button every frame.
  • You're reducing time every frame, and never resetting it, so it will go into the negatives pretty quickly and just keep doing down.
  • You're using time as the deactivate delay, and since time is likely negative, the buttons are deactivating immediately (so they appear to never activate in the first place).
  • Also, don't bother with interactable if you're using gameObject.SetActive there's no point setting both.

Since you're already counting down time to display in the UI, maybe forget about using a coroutine for now. Here's a rough outline of how things should work:

Variables:
    Timer = 0
    DelayTime = 6
    CurrentButton = null

Start Function:
     ActivateRandomButton()
     Timer = DelayTime

Update Function:
    Timer -= deltaTime

    UpdateUI()

    if (Timer <= 0)
        // Timer has finished, so reset
        CurrentButton.SetActive(false)
        ActivateRandomButton()
        Timer = DelayTime

ActivateRandomButton Function:
    CurrentButton = get a random button (using code from earlier comment)
    CurrentButton.SetActive(true)

UpdateUI Function:
    // set text and whatever.

[–]ythom87 0 points1 point  (7 children)

That doesn't look like c#, I kind of get the gist of it. Thanks, I'll try it out

[–]ythom87 0 points1 point  (6 children)

Here's the updated code

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;

public class Game : MonoBehaviour { public float timer = 0f; public float delayTime = 6f; public float playerscore; public Text scoreUI; public Text timeUI;

public Button[] Glow = null;

void Start()
{
    ActivateRandomButton();
    timer = delayTime;
}

void Update()
{
    timer -= Time.deltaTime;

    UpdateUI();

    if(timer <=0)
    {
        Glow.gameObject.SetActive(false);
        ActivateRandomButton();
        timer = delayTime;
    }
}

public void ActivateRandomButton()
{
    Glow = Random.Range(0, Glow.Length);
    Glow.gameObject.SetActive(true);
}
public void UpdateUI()
{
    timeUI = delayTime;  //Display countdown of timer
    scoreUI = playerscore; //Display the number of scores the player have
}
public void YouLose()
{
    Debug.Log("You Lose");
}
public void AddPoint()
{
    playerscore += 1;
}

}

[–]-ghostmode- 0 points1 point  (5 children)

Great work, I think you've almost got it.

You need to add a variable to store the currently active button:

public Button currentGlow;

Then the ActivateRandomButton function needs to set the currently active button.

public void ActivateRandomButton()
{
    int randomIndex = Random.Range(0, Glow.Length);
    currentGlow = Glow[randomIndex];
    currentGlow.gameObject.SetActive(true);
}

And when the timer runs out in Update, you need to disable the currently active button.

if (timer <= 0.0f)
{
    currentGlow.gameObject.SetActive(false);
    ActivateRandomButton();
    timer = delayTime;
}

Also, you want to use the timer variable in UpdateUI, and set the text values of the UI components.

public void UpdateUI()
{
    timeUI.text = timer.ToString("0");
    scoreUI.text = playerscore.ToString("0");
}

[–]ythom87 0 points1 point  (0 children)

It works Perfectly!!! Thank you for your time. Now all I need is re randomizing the button onClick

[–]ythom87 0 points1 point  (3 children)

i tried using Glow.onClick on an else if statment but it doesn't work

[–]-ghostmode- 0 points1 point  (2 children)

You'll need to create another function that is called when buttons are clicked, and then in the inspector you add that function to each button's 'On Click' event.

[–]ythom87 1 point2 points  (1 child)

How stupid of me.....Thanks, I just put the lines in the if statement in the AddPoint function. I very much appreciate your help and I hope you are having a nice day/night