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 KitsuShadow · Jan 31, 2014 at 11:02 AM · c#arrayfloatint

Monodevelop is expecting int from my float array

Unity is giving me an error in regards to my float array. Its expecting int even though i set the array as float. There is an image i uploaded of the error. Thank You

  public class SphereChunk : MonoBehaviour {
 
     public float[,,] map;
     public Mesh visualMesh;
     protected MeshRenderer meshRenderer;
     protected MeshCollider meshCollider;
     protected MeshFilter meshFilter;
 
     // Use this for initialization
     void Start () {
 
         meshRenderer = GetComponent<MeshRenderer> ();
         meshCollider = GetComponent<MeshCollider> ();
         meshFilter = GetComponent<MeshFilter> ();
 
         float width = 10/SphereWorld.currentSphereWorld.radius;
         
 
         map = new float[0.1f, 10f, 0.1f];
         
         for (float x = 0; x < width; x+= SphereWorld.currentSphereWorld.polar) {
             
             for (float y = SphereWorld.currentSphereWorld.radius; y < World.currentWorld.chunkHeight + SphereWorld.currentSphereWorld.radius; y++) {
                 
                 for (float z = 0; z < width; z+= World.currentWorld.chunkWidth * SphereWorld.currentSphereWorld.polar) {
 
                     float a = y * Mathf.Cos(SphereWorld.currentSphereWorld.elev);
                     x = a * Mathf.Cos(SphereWorld.currentSphereWorld.polar);
                     y = y * Mathf.Cos(SphereWorld.currentSphereWorld.elev);
                     z = a * Mathf.Cos(SphereWorld.currentSphereWorld.polar);
 
 
                     map[x, y, z] = 1;
 
                     Debug.Log("X is : " + x + " Y is : " + y + " Z is : " + z);
                     
                 }
             }
         }
     
     }

http://i.imgur.com/mrixalE.png?1

Comment
Add comment · Show 2
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 Sundar · Jan 31, 2014 at 12:53 PM 0
Share

You have declared multidimensional array and giving float values to array. This will help you understand how to declare and use arrays http://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx

avatar image KitsuShadow · Jan 31, 2014 at 12:58 PM 0
Share

yea, i've realized that i want my indices to be floats and indices must be ints. So here is my dilemma. I want to give the array a value at indices x,y,z which is a type of brick. However x,y,z need to be floats. Any suggestions?

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by wibble82 · Jan 31, 2014 at 01:00 PM

Hi There

I think you've misunderstood the functionlity of the float[,,] variable a little.

You would probably recognise the standard definition of an array:

float[] myfloatarray;

This is defining a list of values, each of which is a float. When setting it up you might say:

myfloatarray = new float[10]

Meaning 'I want a list of 10 floats'.

You then get each of those floats by using an 'index' which is always an integer. So you can say 'give me the float at index 4 (i.e. the 5th float)':

afloat = myfloatarray[4]

But there's no such thing as myfloatarray[0.1]!

The code you're using simply defines a 'multi dimensional array', so:

float[,] myfloatgrid = new float[10,10]

is a 10x10 2d grid of numbers.

float[,,] myfloatgrid = new float[10,10,10]

is a 10x10x10 3d grid of numbers.

As before though, you always index it with a integers. In the 2d case its indexed with 2 integers, and in the 3d case its 3 integers.

Without knowing the exact thing you're trying to do I can't quite offer a solution, however that's the root of the errors you're seeing.

-Chris

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
Wiki

Answer by james_170482 · Jan 31, 2014 at 04:16 PM

I think the issue is that you have not set a size for your array.

Unless you know exactly how big the array will need to be your best of using List<>

http://answers.unity3d.com/questions/531953/creating-a-multidimensional-array-list.html

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 KellyThomas · Feb 01, 2014 at 01:28 AM

You can convert from float to int by several techniques.

Each of these has different behaviours:

 float myFloat = 4.5f;
 int x;

 x = (int)myFloat;              // casting will round toward zero
 x = Mathf.FloorToInt(myFloat); // floor will round to the low value
 x = Mathf.RoundToInt(myFloat); // round will round to the closest value
 x = Mathf.CeilToInt(myFloat);  // ceiling  will round to the high value

You will need to decide which is appropriate for your circumstances.

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

Have a problems with a values 1 Answer

Can I create a list with an int/float and a string? C# 2 Answers

Floats and int performance difference? 2 Answers

Convert a char to int / float 2 Answers

float = int / int float value always 0 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