Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 drearyplane · Jul 25, 2015 at 03:50 PM · floatrandom.rangeintcs1502cs0266

Random.Range Problems (Errors CS1502 and CS0266)

I am making a Splat The Rat style game in C#. For this game, I am using Random.Range a lot to determine where, when and what spawns. However, I am getting this mysterious error:

Error CS0266: Cannot implicitly convert type float' to int'. An explicit conversion exists (are you missing a cast?)

So I go and do the step that seems logical and switch the floats to integers in the Random.Range statement changing Random.Range (1.0f, 4.0f); into Random.Range (1, 0);

Then I get error CS1502:

Error CS1502: The best overloaded method match for UnityEngine.Random.Range(float, float)' has some invalid arguments So I switch back to floats. Random.Range (1.0f, 4.0f);` And I get CS0226 again.

Here's my entire script:

 using UnityEngine;
 using System.Collections;
 
 public class Summoner : MonoBehaviour {
 
     public int randomspawn;
     public int randomNumber1;
     public int randomNumber2;
     public int randomnumber3;
     public int randomnumber4;
     public int randomnumber5;
     public int randomnumber6;
 
     public GameObject blueUnicorn;
     public GameObject redUnicorn;
     public GameObject purpleUnicorn;
     public GameObject magentaUnicorn;
     public GameObject pinkUnicorn;
 
     public Transform spawnpos;
 
     //spawn one = 2.4 3.12 0.77
     //spawn two = 2.5 3.24 0.77
     //spawn three = -2.25 -1.44 -0.77
     //spawn four = 2.59 -1.32 -0.77
 
 
 
 
     
     void Update () {
     
         //choosing where to spawn
 
         randomspawn = Random.Range (1, 4);
 
         if (randomspawn == 1) {
 
             spawnpos.transform.position = new Vector3 (2.4f, 3.12f, 0.77f);
     
 
         }
 
         if (randomspawn == 2) {
 
             spawnpos.transform.position = new Vector3 (2.5f, 3.24f, 0.77f);
 
         }
 
         if (randomspawn == 3) {
 
             spawnpos.transform.position = new Vector3 ( -2.25f, -1.44f, -0.77f);
 
         }
 
         if (randomspawn == 4) {
 
             spawnpos.transform.position = new Vector3 (2.59f, -1.32f, -0.77f);
 
         }
 
     //choosing where to spawn end.
 
         //chossing if to spawn
 
         /*
          *Picks a random number 1-10
          *
          *if it equals 5, it goes ahead.
          * 
          * it picks a random number, 1 or 2
          * 
          * if it is two, it summons a blue unicorn.
          * 
          * if it is 1, it goes for red.
          * 
          * it keeps going through until it reaches pinks.
          */
 
         randomNumber1 = Random.Range (0.0f, 10.0f);
 
         if (randomNumber1 == 5) {
 
             randomNumber2 = Random.Range (1.0, 2.0);
 
             if (randomNumber2 == 2) {
 
                 Instantiate (blueUnicorn, spawnpos.position, spawnpos.rotation);
 
             
             }
 
             if (randomNumber2 == 1) {
 
                 randomnumber3 = Random.Range (1.0, 2.0);
 
                 if (randomnumber3 == 2){
 
                     Instantiate (redUnicorn, spawnpos.position, spawnpos.rotation);
 
                 }
 
                 if (randomnumber3 == 1){
 
                     randomnumber4 = Random.Range (1.0, 2.0);
 
                     if (randomnumber4 == 2); {
 
                         Instantiate (purpleUnicorn, spawnpos.position, spawnpos.rotation);
 
                     }
 
                     if (randomnumber4 == 1); {
 
                         randomnumber5 = Random.Range (1.0, 2.0);
 
                         if (randomnumber5 == 2){
 
                             Instantiate (magentaUnicorn, spawnpos.position, spawnpos.rotation);
 
                         }
 
                         if (randomnumber5 == 1){
 
                             randomnumber6 = Random.Range(1.0, 2.0);
 
                         
 
                         }
 
                         if (randomnumber6 == 2) {
                             
                             Instantiate (magentaUnicorn, spawnpos.position, spawnpos.rotation);
                             
                         }
 
                         if (randomnumber6 == 1){
 
                             return;
                         }
 
                     }
                 }
 
             }
 
 
 
 
 
                            
         } else {
 
             return;
         }
 
         
         
 
     }
 }
 

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by tanoshimi · Jul 25, 2015 at 09:05 AM

Use ints everywhere.

 public int randomspawn;  << ok
 randomNumber1 = Random.Range (0.0f, 10.0f); << not ok
 if (randomNumber1 == 5) << ok
 randomnumber4 = Random.Range (1.0, 2.0); << not ok

Comment
Add comment · 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

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

2 People are following this question.

avatar image avatar image

Related Questions

How to submit float score as time to Game Center? 1 Answer

how to make a float value,debug as int.(or show in text UI as Int) 1 Answer

Does genericList.Sort() go low to high? or high to low? - for ints/floats 1 Answer

How do you convert Random.Range to two decimal places? 1 Answer

What the hell? Cannot convert float to int PROBLEM. 2 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