Jump to content
  • Advertisement
Sign in to follow this  
Bobby Lawrence

Unity Need help with a Random Number Generator in Unity C#

This topic is 712 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Greetings and Salutations!

 

I am hard at work on a mobile game project for IOS and have come to a crucial portion of development. The procedurally generated levels. My code is functional at the moment but it seems no matter what I try, I cannot get the Level layouts to vary from stage to stage. Ideally I would seed the RNG with the build index number(or another variable that varies stage to stage but is unique to each stage.) I want each stage's layout to appear random upon first encounter, and be the same each time the stage is entered. Below is the code I am using.  I am new to RNGs and am in my first year of programming, so definitely a novice. I have read up a bit about seeding, cryptographic RNG and various other techniques but everything I find online is outdated information that no longer works or at least I cannot get it to work in the current version of unity. Oh and as the title says I am using C#. Thank you for your help!

 

    public GameObject asteroid10hp;
    public GameObject asteroid20hp;
    public GameObject asteroid30hp;

 

    public int chance10hp;
    public int chance20hp;
    public int chance30hp;
 
    private GameObject chosenGameObject;

 

 

 void Start()

    {
        n = UnityEngine.Random.Range(1, 100);
    }
void SpawnUntilFull()
    {
        
        Transform freePosition = NextFreePosition();
        if (freePosition)
        { 
            
            MoveRandomly();
            RandomAngleGen();
            if (n <= chance30hp) { chosenGameObject = asteroid30hp; }
            else if (n <= chance20hp) { chosenGameObject = asteroid20hp; }
            else if (n <= chance10hp) { chosenGameObject = asteroid10hp; }
                if (n <= spawnChance)
                {
                    LevelManager.breakableCount += 1;
                    GameObject destructible = Instantiate(chosenGameObject, (freePosition.position + moved), Quaternion.Euler(0, 0, angle)) as GameObject;
                    destructible.transform.parent = freePosition;
                }
            
            if (NextFreePosition())
            {
                Invoke("SpawnUntilFull", spawnDelay);
            }
        }
    }
       Transform NextFreePosition()
        {
            foreach (Transform childPositionGameObject in transform)
            {
        
            if ((childPositionGameObject.childCount == 0) & (!childPositionGameObject.CompareTag("Attempted")))
                {
                    childPositionGameObject.tag = "Attempted";
                    return childPositionGameObject;
                }
            }
            return null;
        }

 

Share this post


Link to post
Share on other sites
Advertisement

I don't know very much about Unity, but I came across some difficulties with random numbers in C# too. Sometimes it generates the same numbers because you create a ton of random numbers during the same millisecond (with time-dependent random generators), or because you use the same seed over and over again, which leads to the same list of numbers.

When do you invoke Start()? When you start the game or when you start the level?

Edited by fedeTibaldo

Share this post


Link to post
Share on other sites

The script in question is attached to the parent of the positional game objects I use to spawn the relevant gameObjects. So it Start() is called at the beginning of each level.

Share this post


Link to post
Share on other sites

Probably UnityEngine.Random needs seeding with the system time at the start of the game. It might be seeding to a default value which is the same every execution. (The docs are surprisingly vague about this.)

Share this post


Link to post
Share on other sites

Probably UnityEngine.Random needs seeding with the system time at the start of the game. It might be seeding to a default value which is the same every execution. (The docs are surprisingly vague about this.)

This is my confusion. The docs seem to suggest that it seeds it to the system time but I am getting the same level layout at every level no matter when I load the levels. So it clearly is not doing that in the editor.Thank you, I will see if I can get it seeded with system time.(I'll be back if I can't figure it out)

Share this post


Link to post
Share on other sites

n isn't global, is it?

EDIT: I haven't seen Kylotan answer. Forget me

Edited by fedeTibaldo

Share this post


Link to post
Share on other sites

I wonder if it's something silly like it using the Editor's start time, as opposed to the time at which you hit Play in the editor.  I'd see if setting it yourself somewhere manually would be enough.

Share this post


Link to post
Share on other sites

There's a seeding function: https://docs.unity3d.com/ScriptReference/Random.InitState.html

 

Maybe I'm not understanding the question?

From the documentation it somewhat implies that it already does this:  Random.InitState(startupTime) as a default somewhere*.  But it doesn't appear to be so, at least according to the OP.  I haven't tested it myself.

 

Even the documentation is kind of hazy: 

 

 

The seed is normally set from some arbitrary value like the system clock before the random number functions are used. 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!