- Home /
The question is answered
[Solved] Help with a simple endless level generator.
Hi, Im a beginner coding and im programming a very simple procedural map (idk even if this is a procedural generation lol) but i got stuck. I have two scripts, the first one instantiate the "Ground" gameObject and the other destroy the previous "Ground" gameObject. But this happens: https://imgur.com/hwctmRM
My scripts:
PlayerCollisions
using UnityEngine;
public class PlayerCollisions : MonoBehaviour
{
public GameObject Spawner;
public GameObject Ground;
void OnTriggerEnter(Collider other)
{
switch (other.tag)
{
case "InstantiateGround":
Spawner.GetComponent<GroundSpawner>().SpawnGround();
break;
case "DestroyGround":
Ground.GetComponent<GroundDestroyer>().DestroyGround();
break;
}
}
}
GroundSpawner
using UnityEngine;
public class GroundSpawner : MonoBehaviour
{
public GameObject Ground;
public Collider PlayerCollider;
Vector3 GroundLong;
//Awake gets the scale of the Z coord and stores it in GroundLong vector.
void Awake()
{
GroundLong = new Vector3(0, 0, Ground.transform.localScale.z);
}
//----------------------------------Custom Funtions----------------------------------
//SpawnGround instantiate the next ground by adding GroundLong vector to the current ground position.
public void SpawnGround()
{
Ground = Instantiate(Ground, Ground.transform.position + GroundLong, Ground.transform.rotation);
}
}
GroundDestroyer
using UnityEngine;
public class GroundDestroyer : MonoBehaviour
{
//----------------------------------Custom Funtions----------------------------------
//DestroyGround destroy the previous Ground.
public void DestroyGround()
{
Destroy(gameObject);
}
}
Answer by JustAbhi · Apr 12, 2020 at 02:02 PM
I might have found your problem.
In the script GroundDestroyer you have not passed which gameobject to destroy.
Hope this helps.
Thanks you for your help but i found a easier way to solve this problem. I modified the GroundDestroyer script to:
using UnityEngine;
public class GroundDestroyer : $$anonymous$$onoBehaviour
{
//OnBecameInvisible destroy the previous Ground when it is behind the camera.
void OnBecameInvisible()
{
Destroy(gameObject);
}
}
Now if the Ground is behind the camera destroys himself. (https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnBecameInvisible.html)
Thanks you again for helping me anyways!