Can someone please help me figure out how to make it so when I click play it only spawns one object because when I click play it spawns a ton of objects that fill up the space. please help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnObject : MonoBehaviour {
public Vector3 center;
public Vector3 size;
public GameObject KeyPrefab;
// Use this for initialization
void Start () {
SpawnKey();
}
// Update is called once per frame
void Update () {
}
public void SpawnKey()
{
Vector3 pos = center + new Vector3(Random.Range(-size.x / 2, size.x /2), Random.Range(-size.y / 2, size.y / 2), Random.Range(-size.z / 2, size.z / 2));
Instantiate(KeyPrefab, pos, Quaternion.identity);
}
void OnDrawGizmosSelected()
{
Gizmos.color = new Color(1,0,0,0.5f);
Gizmos.DrawCube(center, size);
}
}