Help with simple generation problem
Im trying to make a randomly generated road. I figured I could make several different types of road "tiles" that are the same size and write a script that puts these pieces together randomly each time. But I am new to programming and dont necessarily know how to do that so I wanted to start with a script that would just start with instantiating 5 roads onto my scene. I thought I had figured it out and I am getting a null reference exception error on the line where I have an if statement in my update function and I cant really figure out why... I have placed my code below so hopefully you can help... I have two separate classes, one( SpawnerManager) attached to an empty game object and RoadSpawner which is attached to each spawnpoint which is a child of every road.
public class SpawnerManager : MonoBehaviour
{
public int spawnedRoads;
public void AddRoad()
{
spawnedRoads++;
}
}
public class RoadSpawner : MonoBehaviour
{
// Start is called before the first frame update
public GameObject straightRoad;
private bool isRoadSpawned = false;
private SpawnerManager spawnManager;
void Start()
{
}
// Update is called once per frame
void Update()
{
if(spawnManager.spawnedRoads < 5)
{
SpawnRoads();
}
}
void SpawnRoads()
{
Instantiate(straightRoad);
isRoadSpawned = true;
spawnManager.AddRoad();
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("SpawnPoint") && other.GetComponent<RoadSpawner>().isRoadSpawned == true)
{
Destroy(gameObject);
}
}
}
Your answer
Follow this Question
Related Questions
how to randomly move each generated clone? 0 Answers
Clear path from A to B 4 Answers
Problem of procedural generation using randomness 2 Answers
Easy chunk prefab creation 0 Answers
How to generate a random 2d world for a 2d endless runner 0 Answers