using UnityEngine;
using System.Collections;
public class spawn : MonoBehaviour {
//This initalises ‘Player Prefab’ to mean whatever asset you want in the actual editor as a intiger (int).
GameObject PlayerPrefab;
public int WaitTime = 10;
public int repeatrate = 0;
public int DeathTime = 20;
//‘voids’ are the fundamental in scripts. They’re a kind of function (which is what they’re called in Jarva Script)
//The start void will run the code on startup.
//The spawn will run according to the “InvokeRepeating”.
void Start()
{
//InvokeRepeating is what enables the ‘Spawn’ function to run according to the ‘spawnWaittime’
InvokeRepeating(“Spawn”, WaitTime, repeatrate);
}
void Spawn()
{
//The spawning code. The ‘Vector3’ line initialises the ‘position’ variable and makes the object
//Spawn with a certain y and x value but the z value is randomly inbetween two numbers (Random.Range)
Vector3 position = new Vector3 (3.75389f,-1.7257f,Random.Range(-5.0f,14.0f));
//Instantiate essentially means spawn. It has 3 input points: asset, location (x,y,z), Rotation
Instantiate(PlayerPrefab, position, Quaternion.identity);
//This destroys a an object or asset within a certain time frame e.g. Destroy(object, Time)
}
}
I’ve got a lot of comments explaining how the code works. It was working as it should and then it just broke randomly. I don’t know why. Here is the error. Its really long: Range is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour ‘spawn’ on game object ‘CustomerH’.
See “Script Serialization” page in the Unity Manual for further details.
UnityEngine.Random:Range(Single, Single)
spawn:.ctor() (at Assets/Scripts/spawn.cs:8)
I also understand that it says to refer to the unity manual but the stuff in the manual is confusing me too. I’m just having trouble deciphering this.