Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Foopa · Sep 06, 2017 at 06:08 AM · errorserialization

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bobisgod234 · Sep 06, 2017 at 06:44 AM 0
Share

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.

avatar image unit_nick bobisgod234 · Sep 06, 2017 at 06:46 AM 0
Share

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?

avatar image JedBeryll bobisgod234 · Sep 06, 2017 at 07:24 AM 0
Share

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...

avatar image Foopa · Sep 06, 2017 at 06:48 AM 0
Share

This is all of the code. That is why I'm confused.

avatar image
0

Answer by Foopa · Sep 06, 2017 at 06:46 AM

Nvm. The error re-appeared as quickly as it disappeared.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JedBeryll · Sep 06, 2017 at 07:22 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

95 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges