Spawn object at a random location from list

Hi all,

I’m looking for help please, bashed my head against the wall as I just can’t figure the code out. And I’m getting nowhere with tutorials.
My game is to delivery as many pizza’s within a time limit. I got the timer and car sorted, the point arrow kinda works but not the biggest problem. Without the checkpoint markers I don’t have a game at all.

I’ve placed a trigger object(Checkpoint) which when the player moves into it adds a point to the scoreboard.
What I would like instead is the object to spawn at a random location from a list of spawn point. So it picks at random one of the spawn points, spawns, waits for the player to reach it, then respawns at a new random location. Kinda of like Crazy Taxi or Simpsons Road Rage.

I apologise for the poor drawing, as you can tell I’m great at MS Paint.
This is my first time coding on Unity so feel free to talk to me like I’m a complete noob.

Thank you to anyone who helps.

Hey there,
Try this if you haven’t already.


Step-by-Step Breakdown:

  1. Set up your spawn points:

In your scene, create several empty GameObjects where you want the checkpoint to appear.

Name them something like SpawnPoint_1, SpawnPoint_2, etc.

Group them all under an empty parent GameObject called CheckpointManager (for clarity).


  1. Create a script called CheckpointManager.cs:

using UnityEngine;

public class CheckpointManager : MonoBehaviour
{
public Transform spawnPoints;
public GameObject checkpointPrefab;
private GameObject currentCheckpoint;

void Start()
{
    SpawnCheckpoint();
}

public void SpawnCheckpoint()
{
    if (currentCheckpoint != null)
    {
        Destroy(currentCheckpoint);
    }

    int index = Random.Range(0, spawnPoints.Length);
    Transform spawn = spawnPoints[index];

    currentCheckpoint = Instantiate(checkpointPrefab, spawn.position, Quaternion.identity);
    currentCheckpoint.GetComponent<Checkpoint>().manager = this;
}

}


  1. Create a script called Checkpoint.cs:

using UnityEngine;

public class Checkpoint : MonoBehaviour
{
public CheckpointManager manager;

void OnTriggerEnter(Collider other)
{
    if (other.CompareTag("Player"))
    {
        ScoreManager.Instance.AddPoint(); 
        manager.SpawnCheckpoint();
    }
}

}


  1. Tag your player GameObject with “Player”.

Make sure the checkpoint prefab has a Collider set to “Is Trigger” and that it’s assigned in the CheckpointManager inspector, along with the array of spawn points.


  1. (Optional) Simple ScoreManager.cs if you don’t have one yet:

public class ScoreManager : MonoBehaviour
{
public static ScoreManager Instance;
public int score = 0;

void Awake()
{
    if (Instance == null)
        Instance = this;
    else
        Destroy(gameObject);
}

public void AddPoint()
{
    score++;
    Debug.Log("Score: " + score);
}

}

Attach this to a new empty GameObject named ScoreManager.


Once this is all set up, your checkpoint will randomly spawn, wait for the player, add a point, then move to a new random location — just like the delivery point system in Crazy Taxi or Simpsons: Road Rage.

Hope this helps get you moving forward! Let me know if you run into any snags or want help linking it to a UI score display.

So that worked like a treat thank you! thankfully the scoring setup I got still works.
I have a few things additional to ask if you don’t mind.

First I got an arrow pointer which doesn’t work now cause the checkpoints that are made in game are clones, so it doesn’t register them. Below is the code I’m using, Are you able to tell me what I need to change to make it work?

Secondly I’m going to have a go at making it so the player has limited pizza’s, it’ll be displayed on screen as a number. when the player reaches a checkpoint, it will remove X number of pizzas from a random value of 1 to 6. As well as there will be a marker where player can refill the pizza count. If I struggle with this will you be able to help?

Thirdly, if you got the time are you able to explain your code above? (The checkpoint random location) As I don’t understand why the code is so small and seemingly simple. And would you be okay if I shared this code publicly as I’m amazed I haven’t found code like this anywhere.

Update on the limited pizzas and refill

I got the refill working but not the random pizza subtraction.

This is what I’m down to and unsure where to go from here, sadly this doesn’t work as I thought.
currentPizzas -= Random.Range(1, 6);

In what way? :face_with_raised_eyebrow:

Also, per the documentation: if you use integers with Random.Range, the max value is “exclusive,” meaning it becomes one less than what you enter. So in your code above for example, Random.Range(1, 6) will return a random number between 1 and 5, rather than 1 and 6.

Unity Problem : u/Pukka_Pad

I’ve posted a video so it’s easier to see.
When going through a checkpoint you can see it takes away 12 pizzas and adds 4 deliveries.
What I would like is it to add 1 delivery and take away a value between 1 and 6.
Hope this helps, sorry for being a pain.

Looks to me like the same thing is being called multiple times, rather than just once. I would debug your collision logic and try to determine exactly why that’s happening. Start by placing a Debug.Log call inside your collision method, before any checks are performed. You can have it say something like “Collision occurred!” Then place one inside the code block where the player tag check is performed (you can have this one say something like “Collided with player!”). When your game runs and you hit a checkpoint, look in the console and see how many (and which) of those logs appear.

Once you figure out exactly why the problem is happening, that’s when you can effectively work towards a solution.

I was running the deliveries, pizzas, countdown etc. all on one script. So I imagine somewhere in there something was causing issues.
I separated into individual scripts and it did work but now seems to off broke again.

The other problem is the rocket(ArrowPointer) isn’t following where the checkpoints are because they are clones in game, any idea on how to fix?

I put the code I’m using is in this blogpost and the video shows clearly that the rocket is just pointing towards the centre of the map.