• 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
1
Question by SgtHobbs · Jun 21 at 04:59 PM · scripting problemerrorerror messagerandom.range

Can Someone please explain how to fix this error, I've seen people use random but it won't let me

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SpawnManager : MonoBehaviour
 {
     public float SpawnRangeX = 9.7f;
     public float SpawnPosY = 4f;
     public GameObject[] EnemyPrefabs;
     public float SpawnDelay = Random.Range(0, 1);
     public int RandomNum;
     public Vector3 SpawnLocation;
 
     // Start is called before the first frame update
     void Start()
     {
         SpawnEnemies();
     }
 
     // Update is called once per frame
     void Update()
     {
         RandomNum = Random.Range(0,EnemyPrefabs.Length);
         SpawnLocation = new Vector3(Random.Range(SpawnRangeX, -SpawnRangeX), 0, SpawnPosY);
     }
 
     private void SpawnEnemies()
     {
         
         Instantiate(EnemyPrefabs[RandomNum], SpawnLocation, EnemyPrefabs[RandomNum].transform.rotation);
 
     }
     
 }
 ![alt text][1]


[1]: /storage/temp/197603-error.png

error.png (7.7 kB)
Comment
Add comment · Show 1
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 onetimepad · Jun 21 at 06:39 PM 1
Share

take note, that Start() is called first, then Update() -> you call Instantiate(...) before Random.Range(...) Edit: also public float SpawnDelay = Random.Range(0, 1); is a problem Move it to Start() or Awake()

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by gliiv_ · Jun 21 at 06:57 PM

@qarallax is correct in moving the random functions from Update() into Start(), as it would yield better performance, but the issue lies in the constructor for your SpawnDelay variable. Unity doesn't like it when you try to give a freshly constructed variable a random range, so you'll have to do it in Start() or Awake(). I've applied the relevant fixes to your code and recommend you compare it to the code you wrote to get better context, but here it is below!

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     
     public class SpawnManager : MonoBehaviour 
     { 
         public float SpawnRangeX = 9.7f;
         public float SpawnPosY = 4f;
         public GameObject[] EnemyPrefabs;
         public float SpawnDelay;
         public int RandomNum;
         public Vector3 SpawnLocation;
     
          // Start is called before the first frame update
         void Start()
         {
             SpawnEnemies();
             RandomNum = Random.Range(0,EnemyPrefabs.Length);
             SpawnLocation = new Vector3(Random.Range(SpawnRangeX, -SpawnRangeX), 0, SpawnPosY);
             SpawnDelay = Random.Range(0, 1);
         }
         // Update is called once per frame
         void Update()
         {
     
         }
         private void SpawnEnemies()
         {        
             Instantiate(EnemyPrefabs[RandomNum], SpawnLocation, EnemyPrefabs[RandomNum].transform.rotation);
         }
     }
 
Comment
Add comment · Show 5 · 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 SgtHobbs · Jun 21 at 07:03 PM 0
Share

it's still saying the same error as I linked to my question

avatar image gliiv_ SgtHobbs · Jun 21 at 08:06 PM 0
Share

That is incredibly strange. I've tested this code in my own Unity project to make sure I wasn't going crazy, and I'm not getting the error in question. Are you sure you have modified the code to correct the error / saved the file? Not trying to be mean (I forget to save more often than I care to admit myself lol) and the error text specifically gives us the reason for the error, in that you aren't allowed to use Random.Range in a constructor. If you could post the code you have currently here (after applying the requisite fixes, I wouldn't expect you to post literally the same exact code) and we can take a look and see where the breakdown is.

avatar image qarallax gliiv_ · Jun 22 at 12:20 PM 1
Share

Yeah its strange. As @gliiv_ say the line causing the error should be this:`public float SpawnDelay = Random.Range(0, 1);`

avatar image Bonfire-Boy SgtHobbs · Jun 23 at 01:54 PM 1
Share

This is the correct answer (the error message is telling us exactly what you've done wrong), so please don't give up on it.


If you have made the change @gliiv has shown you, removing the call to Random.Range() from the field initialiser, and you're still getting the same error, then IMO you've probably made the same mistake somewhere else.

avatar image Bunny83 · Jun 22 at 02:43 PM 1
Share

You shouldn't call SpawnEnemies before you roll your random numbers... That doesn't make much sense. However the issue indeed is the usage of Random.Range inside the field initializer, well, just as the error tells you ^^.


Though having a single script that only spawns a single enemy probably doesn't make much sense. The rolling of the random numbers probably should be inside the SpawnEnemies method before the Instantiate call. That way you can spawn a new random enemy by simply calling that method again. How, when and how often is of course up to the OP. Though you usually don't want to spawn one new enemy every frame.

avatar image
0

Answer by qarallax · Jun 21 at 05:35 PM

Try to put "RandomNum = Random.Range(0,EnemyPrefabs.Length);" in the start method instead of the update method. And make sure to put it before you call the spawnEnemies void. Right now you are changing RandomNum every frame.

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 SgtHobbs · Jun 21 at 05:37 PM 0
Share

The same error still shows up

avatar image qarallax SgtHobbs · Jun 21 at 05:39 PM 0
Share

Actually you should do that with all the lines in the update method.

avatar image SgtHobbs qarallax · Jun 21 at 06:16 PM 0
Share

Still no luck, been hours and I have no clue. I'm a beginner but damn

avatar image Bonfire-Boy · Jun 23 at 01:57 PM 1
Share

This answer is incorrect. Having that call in Update() will not cause the error shown.


As other answers state, it is the call to Random.Range in the field initialiser that is the issue.

avatar image
-1

Answer by Lennyy96 · Jun 21 at 06:50 PM

Maybe it‘s because you first call spawn enemies and then you set randomNum.

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 SgtHobbs · Jun 21 at 07:04 PM 0
Share

I've changed it as said by other people but it still doesn't work.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Script Error 1 Answer

error CS0117: `UnityEngine.Graphics' does not contain a definition for `DrawProceduralIndirectNow' 0 Answers

How do I fix this problem-it won't let me start my game in Unity because of an override problem 1 Answer

My Script Wont work? 2 Answers

Error "OnTriggerEnter: this message has to be of type: Collider" 0 Answers


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