Random Level Generation Not Working

Hi there guys! So, I have been trying to make random level generator so that my level is gonna be infinite and not repeated, and right now I cannot seem to understand why I get no spawning of things even though I have placed them inside the LevelManager GameObject where they are supposed to be and I am only getting this warning : "Unnecessary assignment of a value to “lastlevelPartTransform”.

Here is my code :

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

public class LevelManager : MonoBehaviour
{

    [SerializeField] private Transform LevelPart1;
    [SerializeField] private Transform LevelStart;
    private void Awake()
    {
        Transform lastlevelPartTransform;
        lastlevelPartTransform = SpawnLevelPart(LevelStart.Find("EndPosition").position);
        lastlevelPartTransform = SpawnLevelPart(lastlevelPartTransform.Find("EndPosition").position);
        lastlevelPartTransform = SpawnLevelPart(lastlevelPartTransform.Find("EndPosition").position);
        lastlevelPartTransform = SpawnLevelPart(lastlevelPartTransform.Find("EndPosition").position);
        lastlevelPartTransform = SpawnLevelPart(lastlevelPartTransform.Find("EndPosition").position);
        lastlevelPartTransform = SpawnLevelPart(lastlevelPartTransform.Find("EndPosition").position);
    }

    private Transform SpawnLevelPart(Vector3 spawnPosition)
    {
        Transform levelPartTransform = Instantiate(LevelPart1, spawnPosition, Quaternion.identity);
        return levelPartTransform;
    }
}

What do you guys think I could do to fix this? At the moment I was just testing if the spawning works properly and now I’m stuck here when I tried to spawn 5 parts :confused:
Any suggestions?
Thanks! :slight_smile:

Code works for me. Doesn’t show any error like you indicate, spawns a string of six (6) LevelPart1 prefabs offset by the distance of the “EndPosition” child object in it. That error may be coming from another part of your code, or else you have other problems in your prefabs.

NOTE: the LevelStart object is never cloned. Its EndPosition object is simply used as the first position. Not sure if you mean that.

You should also investigate loop constructs because then you can shorten lines 14 to 18 to a for() loop.

Oh well I wanted to also clone the LevelStart once at the beggining but I don’t know why it doesn’t work like this for me.Other scripts don’t relate to this other than a cirlce sprite that changes the color or the car when triggered.Other than that in my obstacle prefabs there is just the platforms themselves and some other spikes that trigger death.

Did you assign values to LevelPart1 and LevelStart? Do they both contain an object called EndPosition? Is there anything that could cause them to be destroyed after being spawned?

How are you testing to see if they’ve been created? If you just look in the game view, you might miss them if they are inactive or small or in the wrong place, so I suggest you make sure you look in the object hierarchy.

If you can’t figure out what’s happening, start adding Debug.Log calls to narrow down when things start going wrong.

The only cloning you’re doing is line 23 above, and that explicitly lists LevelPart1 as what it makes.

Perhaps you intend to pass in the thing you want to clone so you can do:

Start
Part
Part
Part
Part
etc.

?

Procedural generation stuff is hard and can bend your brains at time, but I find it very rewarding. Not only that but I see you’re already thinking ahead by using things like that EndPosition object to indicate where the next part would go. Bravo!

1 Like

If you want an arbitrary number of parts, such as for an endless runner, one excellent approach that works nicely in Unity is:

  1. keep a Vector3 that is your “cursor” or where the new level is being created.

  2. in the update loop create enough parts to get the cursor off the side of the screen “enough” (whatever that means). Each time you make a part, move the cursor along.

  3. as the player moves along, the player can move the camera and adjust the side of the screen, and meanwhile the level generation script will check every frame and suddenly say “Oh man, the camera has almost caught up to the cursor, I gotta make another chunk!” and stay ahead of it.

  4. and you can put another cleanup script “behind” the scrolling scene that looks for chunks too far left and Destroy()s them, but that can wait until you get more stuff going.

Keep at it!

1 Like

Well after getting home and getting this to work based on the awesome suggestions :slight_smile:
I will try to maybe make it so it could be based on difficulties but I gotta think a while haha.I have been using C# and Unity for about 3 weeks and I’m kind of trying to get the hang by myself of how to do things but sometimes I get stuck haha
It’s still fun though, gotta keep trying! :smile:
I’m gonna be back with an update after I arrive home.

1 Like

Yeah for now is just some sort of testing that I’m trying, I’ll also try to look into the Z values, maybe they got messed up and my camera can’t see them, but I doubt it as my car just keeps falling and it doesn’t seem that it could be the main issue.
And thank you very much! :slight_smile:
I appreciate everything.
It’s more exciting when it works from the first try, but problems make it more interesting sometimes haha

1 Like

The obstacle prefabs are placed in the inspector itself, and they both have an object called EndPosition.They can’t be destroyed by anything at this point from other scripts, so I don’t know what happens.
I do get an error in the console though that is red saying the following :

NullReferenceException : Object references not set to an instance of an object. LevelManager.Awake() (at assets/Scripts/LevelManager.cs:14

Do you have any idea about what that could mean?

Well at this point I’m still stuck with this error that I can’t get rid of somehow… :confused:

NullReferenceException : Object references not set to an instance of an object. LevelManager.Awake() (at assets/Scripts/LevelManager.cs:14

Using the code that I provided in my first post.
I have also tried to use your suggestion but I don’t think that I have coded it well or something similar still happens that’s making it not instantiate things. And I still get the error above even there.
Am I doing something wrong?

Forgot to mention this but, I have done some more testing meanwhile trying different stuff, and it seems like things work well if I try to instantiate my levelpart like this :

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

public class LevelManager : MonoBehaviour
{

    [SerializeField] private Transform LevelPart1;
    private void Awake()
    {
        SpawnLevelPart(new Vector3(45, 9));
        SpawnLevelPart(new Vector3(45, 9) + new Vector3(45, -4));
        SpawnLevelPart(new Vector3(45, 9) + new Vector3(45 + 45, -4));
        // And so on.Things work if I do it like this
    }

    private Transform SpawnLevelPart(Vector3 spawnPosition)
    {
        Instantiate(LevelPart1, spawnPosition, Quaternion.identity);
    }
}

Everything goes right, but if I try and change things to adapt it in some way to make it spawn them at an EndPoint or spawn them when it’s needed only using a constant that checks if there is a certain distance away so it knows when it’s needed to spawn more but didn’t manage to fix anything like this. :confused:
Could this hint to something that is broken if it’s working in this state but not if I advance further?
I hope so but I can’t seem to have a proper idea for fixing this yet. :slight_smile:

As Antistone said above, if you checked all the fields are filled out, go make sure you didn’t drag this same script onto a different GameObject (where it isn’t filled out). You can filter in the hierarchy for the script name by prefixing it with t:, such as t:LevelManager . That will show all GameObjects with that script. Maybe you have it in more than one place. Be sure to check your prefabs don’t have a copy on it. It’s a common drag-and-drop mistake.