My random system is not working.

So in my main menu, I have a splash button that when clicked on, takes you to my YouTube page. I also tried to code in that there’s a 1 in 5 chance it sends you to a rickroll.

The code i used for it is as following(I only included the part thats not working how I intend)

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

public class MainMenu : MonoBehaviour
{
    public void SplashButton()
    {
        float FunValue = Random.Range(1f, 5f);

        if (FunValue == 5f)
        {
            Application.OpenURL("https://www.youtube.com/watch?v=dQw4w9WgXcQ&ab_channel=RickAstley");
            Debug.Log("Get rickrolled lol");
        }

        if (FunValue < 5f)
        {
            Application.OpenURL("My YouTube page.[yeah i know, i dont really want to share a link here]");
            Debug.Log("Ma YT page!!1");
        }

        Debug.Log("SplashButton was clicked.");
    }
}

When clicked on the button that runs SplashButton() , it does take you to my youtube page. But when I tried it out, the rickroll thing never comes. I have tried it at least 20 times, it doesn’t work.

I do think it has to do with the fact I messed up Random. I don’t get compiler errors, but like I said, the rickroll is not coming. What’s going on here?

Use the integer version of Random.Range().

You’re using the floating point version so you essentially never get it equal to 5f.

Beware the integer version if you ask from (1,5) you will get 1,2,3,4, but not 5, so you always need an extra end number.

int choice = Random.Range( 1, 6); // 1 to 5
1 Like

If you do something like this make it rare. So rare people believe they made a mistake when it happens.

Took the community literally years to figure out that one in ten thousand Minecraft title screens actually reads Minceraft.

2 Likes

It worked! Thanks man!

1 Like