Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 W1k3 · Jan 29, 2015 at 04:46 AM · c#errorarraylistconversion

Cannot convert float to int when only using floats (C#)

I have a list called layouts that stores 2 dimensional arrays of floats, but whenever I try and access it in the format of layouts[int][float][float] each line gets the 2 identical errors:

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

This happens on line 53, 56, 78.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class floorbuilderv3 : MonoBehaviour {
 
     //room related
     public GameObject[] fourrooms = new GameObject[3];
     public GameObject[] threerooms = new GameObject[3];
     public GameObject[] tworooms = new GameObject[3];
     public GameObject[] onerooms = new GameObject[3];
     public GameObject[] startrooms = new GameObject[1];
 
     //settings
     public float room_distance = 10f;
 
     //flooor related
     int currentfloor = 0;
     public int Size_Increase_Factor = 5;
     List<float[,]> layouts = new List<float[,]>();
     int roomquantity;
     int branchnumb = 0;
     int gridsize;
 
     void Start () {
         transform.position = new Vector3 (0, 0, 0);
         buildnextfloor ();
     }
 
     void buildnextfloor(){
         gridsize = (currentfloor+1) * 20;
         roomquantity = ((currentfloor+1) + (currentfloor+1)) * Size_Increase_Factor;
         float[,] floor = new float[gridsize,gridsize];
         layouts.Add (floor);
         Debug.Log ("gridsize: " + gridsize + ", room quantity: " + roomquantity);
 
         //create unitialized, prioritized floor layout
         creattile (-1, 1,new Vector2(Mathf.FloorToInt (gridsize/2), Mathf.FloorToInt (gridsize/2)));
 
         //initialize the layout
         initialize ();
     }
 /////////////////////////////////////////tile creation////////////////////////////////////    
     void creattile(int remaining, int branch, Vector2 location){
 
         float temp = location.x;
 
         if(remaining > 0)
             return;
         if(location.x > gridsize || location.x < 0 || location.y > gridsize || location.y < 0)
             return;
 
         if(layouts [currentfloor] [location.x, location.y] != 0f)
             return;
 
         layouts [currentfloor] [location.x, location.y] = branch;
         Debug.Log ("placeholder created. grid location: " + location.x + ", " + location.y  + ". branch number: " + branch);
 
         if(remaining == -1){
             int adjacents = Random.Range(2, 4);
             for(int i = 0; i < adjacents; i++){
                 creattile((roomquantity/adjacents),2, expand(location, 0));
             }
         }
         else if(remaining > 2){
             int adjacents = Random.Range(1, 3);
             for(int i = 0; i < adjacents; i++){
                 creattile((remaining/adjacents), branch++, expand(location, 0));
                 branchnumb = branch;
             }
         }
     }
     Vector2 expand(Vector2 location, int iteration){
         iteration ++;
         float x, y;
         x = Mathf.Round (Random.Range ((location.x-1),(location.x+1)));
         y = Mathf.Round (Random.Range ((location.y-1),(location.y+1)));
         if(layouts [currentfloor] [location.x, location.y] != 0 && iteration < 10){
             return expand(location, iteration);}
         return (new Vector2(x, y));
     }
 //////////////////////////////////tile initialization/////////////////////////////////////    
     void initialize(){
         int initializedrooms = 0;
         for(int i = 0; i < branchnumb; i++){
             for(int x = 0; x < gridsize; x++){
                 for(int y = 0; y < gridsize; y++){
                     if(initializedrooms <= roomquantity){
                         if(layouts [currentfloor] [x, y] == i){
                             placeroom(new Vector2(x, y));
                         }
                     }
                 }
             }
         }
     }
     void placeroom(Vector2 gridlocation){
         Debug.Log ("placed room :)");
     }
 }
 
 
 
 
 
 
 
 
 
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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by yashpal · Jan 29, 2015 at 05:35 AM

@W1k3,

Just replace this line

 if(layouts [currentfloor] [location.x, location.y] != 0f)

with this

 if(layouts [currentfloor] [(int)location.x, (int)location.y] != 0f)


hope this helps you.

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
avatar image
1

Answer by Naphier · Jan 29, 2015 at 10:42 AM

Vectors have float components. So layout.x and layout.y need to be converted to integers to be used as indices in an array/list. I think what you mean to do is:

 layouts[currentFloor , 0] = location.x;

I'm not familiar with the syntax you're using on your list. http://www.dotnetperls.com/list might help a lot. If you're only using 2 dimensions I'd do:

 List  layouts = new List;
 Vector2 location = new Vector2 (x,y);
 layouts.Insert(currentFloor , location);

Hope that helps.

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
avatar image
0

Answer by nareshkumaryakkala · Jan 29, 2015 at 10:37 AM

You have used Vector2 type for location. Vector2 values are float type.

So you have to convert location values to int.

Example line 43:

 if((layouts [currentfloor]) [System.Convert.ToInt32(location.x), System.Convert.ToInt32(location.y)] != 0.0f)


My suggestion is create a simple point structure instead of vector2

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

22 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

Related Questions

Problem with List not filling using add method in a foreach loop 1 Answer

Can´t instantiate objects in list correctly 1 Answer

Need my function to work with different lists of different values (classes) 1 Answer

[Solved]List.FindIndex error C# 1 Answer

How to make a or array of GUI.Button's? 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