Unity 3d randomly spawned gap in a wall game object.

Hi! I’m a very beginner in unity 3d and I’m currently working on my first project. I want to spawn a wall with a gap randomly generated somewhere at the wall. What I did is that I divided the wall in three parts which would spawn one by one from the left to the right when the application started. And to randomly generate the wall, I skipped one of the three instantiations depending on the result of an int variable using Random.Range(). This is the script that I did:

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

namespace LevelGenerator {
   

public class LevelGenerator : MonoBehaviour {
   
    // This will be one of the parts of the wall
    public GameObject wallPart;
   
    // Here I take the size of the wall part along the X axis or you can say the length of the wall part
    public float wallSizeX;
   
    // I put an origin point all the way to the left so the wall parts spawn from the origin and go on
    // to the right
    public GameObject OriginPoint;
   
    // This is just a temporary variable, which was supposed to be implemented in the for loop later
    // on this script
    private int s;
   
    // Another temporary variable.
    private float temp;
   
    // This will be the variable which will randomly take a number between 1 and 3
    private int generator;
   
    // As soon as the application starts...
    void Start()
    {
        // the temporary variable would be initialized to 0...
        temp = 0;
       
        // the "generator" variable would take a number between 1 and 3 using Random.Range()...
        generator = Random.Range(1,4);
       
       
        // start the for loop to loop three times (s < 3 when s = 0) because there are 3 wall parts,
        // where one would be skipped for the gap...
        for (s = 0; s < 3; s++)
        {
           
            // check if generator is equal to s, and if so, skip the Instantiation of a wall part, so it
            // leaves a gap...
            if (generator - 1 == s) {
                continue;
            }
           
            // the function which would Instantiate one wall part, starting from the origin point...
            generateWall();

        }
       
    }
   
    // And now the generateObstacle() method...
    void generateWall() {
       
        // it spawns a wall part (wallPart) equal to the locationof the origin point for the first time...
        Instantiate(wallPart, new Vector3(OriginPoint.transform.position.x + (temp * s), OriginPoint.transform.position.y, OriginPoint.transform.position.z), Quaternion.identity);
        // and if s gets bigger than 1, we want temp to be equal to wallSizeX, so when spawning another wall part, it would spawn
        // in the originpoint + the size of the wall along the X axis, thus creating a wall along the X axis...
        if (s >= 1) {
            temp = wallSizeX;
        }
        // I hope you get the Idea
    }
   
}
}

All I wanna know is WHY? The code makes sense, right? Then why, why does it crash unity. All it does is constantly spawning clones of the wall part and crashing the application. I checked if the for loop really stopped after s being bigger than 2 and I even put in an if statement that breaks the loop if s > 2. It still does not work. If somebody knows why it does that, please tell me. It might be a stupid mistake, but I wanna know the answer of this. if you need anything from me, just ask. Thank you!

Does the wall that you spawn ALSO contain this script? Then it will be wall spawning over and over again ever deeper and thicker.

Best thing to do is attach the debugger and see what the code is actually doing. Put your breakpoints in the Start() and also the generateWall() methods, see which hits and how often.

1 Like

I figured it out. I actually had the script attached to the wall itself, so instead, I attached it to another game object and It worked…kind of. Unity did not crash and it did not create copies of itself but the script did not work as I wanted it to. I made the generator variable public so I can see what happens and I found out that when generator = 1 the gap is in the middle instead of the left side where the origin point is. Because it should have skipped when it executes if (generator - 1 == s) continue; So 1-1 = 0, right? Moreover, when generator was 2 or 3 it left the middle and the right side empty. So both objects spawned on the left in the same place. If you know why, please tell me. Also, this might sound a little bit dumb, but I don’t know how to attach the debugger. :confused:

Nevermind, I fixed it. All I had to do was move the following statement to execute first in the loop and not in the generateWall() method. Thanks for your help

  • if (s >= 1) {
  • temp = wallSizeX;
  • }

Maybe you could change OriginPoint to Transform, and put this statement in ‘void start’:
wallSizeX = wallPart.transform.localScale.x;

Please don’t necro posts. This was resolved way back in 2017.

Thanks.