random spawn assigned prefab from collider

Hello I need an script that spawn a assigned prefab from al collider this must be random and the spawning must go faster and faster until the player dies

Rick, typically you need to show some effort on your part before asking the community for help. You can’t expect us to write your game for you.

Programming is a matter of breaking down a large problem into smaller pieces.


Your problem is:

“I need an script that spawn a assigned prefab from al collider this must be random and the spawning must go faster and faster until the player dies”

We can break this down into a number of small problems:

  1. Assigning a prefab to a variable on a script.

  2. Instantiating the prefab.

  3. Instantiating the prefab in a random place.

  4. Instantiation is on a timer, and must increase in frequency.


The video T27M posted is perfect, and addresses points 1&2 very well.

For point 3 we can look at the UnityEngine.Random class: Unity - Scripting API: Random.insideUnitSphere


Point 4 is where things get a little more interesting.

Firstly, we should take a look at the script reference for Time: Unity - Scripting API: Time

You’re going to need to write some kind of emission timer to handle your instantiation intervals:

public class Emitter : MonoBehaviour
{
    private float m_LastEmission;

    // You'll need to adjust this line to get the rate you want
    public abstract float emissionRate { get {return Time.time;} }

    protected void Update()
    {
        float elapsed = Time.time - m_LastEmission;
        int numEmissions = Mathf.FloorToInt(emissionRate * elapsed);

        Emit(numEmissions);

        if (numEmissions <= 0)
            return;

        float emissionTimePerInstance = 1.0f / emissionRate;
        m_LastEmission += emissionTimePerInstance * numEmissions;
    }
}