- Home /
I am getting a random serialization error and don't know why. How do I fix this? What is it saying?
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.
Answer by JedBeryll · Sep 06, 2017 at 06:34 AM
We need to see the full code from the start to at least the Start method because the error points to line 8, and there's nothing wrong with line 8 in this code. In general btw, you should not use constructors in MonoBehaviours (except maybe static constructors) unless you like a headache. Use Awake, Start and OnEnabled instead. Also methods and calculations are not allowed in field initializers. You should try renaming the Spawn method to something else and check if it still gives an error. Don't know how InvokeRepeating works on the inside, it may not be case sensitive and it may call the default constructor of your class. That's all I can think of without seeing more of the code.
Interestingly, the Spawn function is not a constructor, the capitalization is different and it has a return type. It is still definitely worth trying to rename the function anyway, I have a suspicion that this may be a custom error message by unity that is bugged, or something.
Yeah. I removed my answer because @bobisgod234 pointed out my error in saying that Spawn() was seen as a constructor. However do I recall the serialization is case insensitive?
Yeah it's not a ctor but if you don't define one, the default ctor will be provided and if the InvokeRepeating is not case sensitive, it's possible it calls the ctor. I've always hated calling methods by name...
Answer by Foopa · Sep 06, 2017 at 06:46 AM
Nvm. The error re-appeared as quickly as it disappeared.
Try this:
private void Start() {
StartCoroutine(SpawnRepeating(WaitTime, repeatRate));
}
private IEnumerator SpawnRepeating(float initialTime, float repeatRate) {
yield return new WaitForSeconds(initialTime);
while (true) {
yield return new WaitForSeconds(repeatRate);
Spawn();
}
}
EDIT: repeatRate changed to initialTime in the first yield
Your answer
Follow this Question
Related Questions
CreateInstanceFromType is not allowed to be called during serialization 1 Answer
UnityScript How To Deserialize? 1 Answer
URGENT - Serializing error The type of argument is not primitive 0 Answers
Error Using DataContractSerializer 1 Answer
"Serialization depth limit exceeded" with non-serializable classes 1 Answer