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 /
  • Help Room /
avatar image
0
Question by Tatsumi-Kun · Apr 26, 2016 at 09:25 PM · booleanfloatintenumwaves

Really need help please/A switch expression of type `float' cannot be converted to an integral type, bool, char, string, enum or nullable type

I trying to make a enemy ai movement but i keep get a error i can't find a way to fix it. using UnityEngine; using System.Collections;

 public class Waves : MonoBehaviour {
 
     float aTemp;
     float bTemp;
     float dTemp;
     float cTemp;
     float x3Temp;
     float x2Temp;
     float x1Temp;
 
 
     GameObject enemy1;
     float nextWave;
 
     float path;
     
     public float Posy = 0f;
     public float Posx = 3.9f;
     int dirTemp;
     void Start () {
         nextWave = 2.0f;
     
     }
     
     // Update is called once per frame
     void Update () {
         if (Time.time > nextWave) {
             //random number determines the path equation
             path = Mathf.Floor (Random.Range(Posy, Posx));
 
             //random number determines direction of motion
             if (Random.Range (0.0f, 1.0f) < 0.5f) {
                 dirTemp = 1;
             }
             // if(Random.Range(0.0, 1.0) <0.5)
             else {
                 dirTemp = -1;
             }
             //else (Random.Range(0.0, 1.0) < 0.5)
         }
         
 
         switch (path) {
         case 1:
             aTemp = Random.Range (0.02f, 0.1f);
             x2Temp = Random.Range (-6.0f, 6.0f);
             dTemp = Random.Range (-3.0f, 5.0f);
             break;
 
         case 2:
             aTemp = Random.Range (1.0f, 3.0f);
             bTemp = Random.Range (0.2f, 0.4f);
             dTemp = Random.Range (0.0f, 5.0f);
             break;
         
         case 3:
             aTemp = Random.Range (1.0f, 3.0f);
             bTemp = Random.Range (0.2f, 0.4f);
             dTemp = Random.Range (-5.0f, 5.0f);
             break;
         default:
             print ("Waves.cs: Update(): path out of range");
             break;
         }
         //switch (path)
 
         // Instantiate the enemy with starting position and path to follow
         for (float i = 0; i < 5; i++) {
             if (path < 3) {
                 instEnemy (dirTemp * (-12.0f - i * 2.0f), path);
             }// if (path < 3)
             else {
                 instEnemy (14f + i * 2.0f, path); // x= f(y)
             } // else (path < 3)
 
         }// for 1
         nextWave += 5.0f;
         // if (Time.time > nextWave)
         // Update()
     }
 
     void instEnemy (float startPos, float path)
     {
         Vector3 pos = new Vector3(0.0f,0.0f);
         switch (path) {
         case 1:
             pos.x = startPos;
             pos.y = 0.0f;
             break;
         case 2:
             pos.x = startPos;
             pos.y = 0.0f;
             break;
 
         case 3:
             pos.x = 0.0f;
             pos.y = startPos;
             break;
         default:
             print ("Wave.cs: instEnemy(): path out of range");
             break;
         }// switch (path)
         pos.z = 0f;
         Instantiate (enemy1, pos, Quaternion.identity);
         Enemy1 other = GetComponent<Enemy1> ();
         other.path = path;
         other.a = aTemp;
         other.b = bTemp;
         other.c = cTemp;
         other.d = dTemp;
         other.x3 = x3Temp;
         other.x2 = x2Temp;
         other.x1 = x1Temp;
         other.dir = dirTemp;
     } // instEnemy
 }


and having some other error too

Assets/Waves.cs(87,17): error CS0151: A switch expression of type float' cannot be converted to an integral type, bool, char, string, enum or nullable type Assets/Waves.cs(108,23): error CS0266: Cannot implicitly convert type float' to `int'. An explicit conversion exists (are you missing a cast?)

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
1
Best Answer

Answer by tanoshimi · Apr 26, 2016 at 09:39 PM

path is a float. You declare it as such on line 15:

 float path;

But you're using it in a lot of switch statements to compare to integer values - case 1:, case 2: etc. You can't use float values in switch statements as the error message states, because they're inherently imprecise. So just declare it as an int instead:

 int path;

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 Tatsumi-Kun · Apr 26, 2016 at 10:07 PM 0
Share

dude it working find now bro i change it to int and and Change $$anonymous$$ath.FloorToInt

avatar image
0

Answer by Pharaoh_ · Apr 26, 2016 at 09:40 PM

Indeed, floats are limited by switch statements. A workaround is to cast it to int, and, since you are using integers on the switch, then use: switch ((int) path) {}. Alternatively, if you wish to work with decimals, use an if/then/else.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

error CS0266 Cannot implicitly convert type `float' to `int'. An explicit conversion exists (are you missing a cast?) 2 Answers

Error CS0266 with my clicker game 1 Answer

Adding Int's from different and the same script 1 Answer

How to make a number higher than Quintillion? (1000000000000000000) 1 Answer

float number displayed as an int? or at least on the health bar 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