Skip to main content

Prefab + Simple Spawn

we will setup a simple spawn location, from where GameObjects - instances of a Prefab will be instantiated - 'spawned' at a predefined rate.

Creating Prefabs

A Prefab is a pre-made blueprint for a game object that can be used repeatedly to create instances of that object with the same properties and behaviors. We could for example create a prefab of our random walker with the green material and the RandomWalker script attached.

Please first create a Folder called "Prefabs" if you don't have such a folder in Assets.

We create a prefab, by dragging a GameObject from the Hierarchy to the Assets window, ideally into the folder called "Prefabs"

aa_basics_005.gif


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectSpawner : MonoBehaviour
{
    // Start is called before the first frame update
    public GameObject prefab;

    
    public float spawnInterval = 1.0f;


    void Start()
    {
        InvokeRepeating("spawnObject", spawnInterval, spawnInterval);        
    }

    void spawnObject()
    {
    Instantiate(prefab, transform.position, transform.rotation);
    }
}