- Home /
Endless level generation not instantiading
Hello, I am trying to create a vertical level generator for an Endless "Jumper". The way I'm trying to do this is to have prefabs generated and gradually move down. Each of the prefabs contains a "room" with platforms (and eventually pickups) and another empty game object "EndPosition". The parent of each prefab is positioned at the bottom of the room and the "EndPosition" at the top. Also, I have an empty object "PlatformGenerationPoint" positioned slightly above the camera. The plan was to instantiate a bunch of platforms at awake. And then whenever the last platform moves past the "PlatformGenerationPoint" to create additional ones. But in practice, on start, rooms are immediately generated up until the "PlatformGenerationPoint" and stop generating afterwards, despite "EndPosition" of the last room moving past the Point. I am new to Unity, and coding in general, so I might be overlooking something obvious here. Please, help me out. Here is the generator code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelGenerator : MonoBehaviour
{
// Contains and "EndPosition" like in the prefabs
[SerializeField] private Transform levelPart_Start;
//Room prefab
[SerializeField] private Transform levelPart_1;
//PlatformGenerationPoint
[SerializeField] private Transform generationPoint;
private Vector3 lastEndPosition;
private void Awake(){
lastEndPosition = levelPart_Start.Find("EndPosition").position;
int startingSpawnLevelParts = 3;
for (int i = 0; i < startingSpawnLevelParts; i++)
{
SpawnLevelPart();
}
}
private void Update() {
if (lastEndPosition.y < generationPoint.position.y) {
SpawnLevelPart();
}
}
private void SpawnLevelPart() {
Transform lastLevelPartTransform = SpawnLevelPart(lastEndPosition);
lastEndPosition = lastLevelPartTransform.Find("EndPosition").position;
}
private Transform SpawnLevelPart(Vector3 spawnPosition) {
Transform levelPartTransform = Instantiate(levelPart_1, spawnPosition, Quaternion.identity);
return levelPartTransform;
}
}
And here is the code attached to the prefabs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PushDown : MonoBehaviour
{
void Update()
{
StartCoroutine(startCamera());
}
private IEnumerator startCamera()
{
while(true){
transform.position -= transform.up * Time.deltaTime / 300;
yield return null;
}
}
}
Your answer
Follow this Question
Related Questions
How would one go about making an asteroid field in a circle around an empty object in 2D? 0 Answers
Destroying assets is not permitted to avoid data loss. 0 Answers
Instantiating randomly sized prefab to the right of another 2D 0 Answers
Replacing Weapon 2D Shooter Unity 1 Answer
Instantiated objects not appearing in scene or game view 2 Answers