- Home /
Question by
AldevtoStudio · Apr 14, 2020 at 02:30 PM ·
c#scripting problemscripting beginnerdestroy object
Help with a simple procedural generation
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);
}
}
Comment