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!