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"
Creating a spawn location
Create an Empty GameObject with Ctrl+Shift+N or GameObject (top bar) -> Create Empty
Name it "SpawnLocation"
Set its Transform component to 0,0,15 in the Inspector (the position of the Camera at 0 , 0 , 0)
In the Inspector click "Add Component", type SimpleSpawn and press the enter key to create a new Script with the name "SimpleSpawn"
Copy the following code into the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SimpleSpawn : 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);
}
}
No Comments