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 /
This question was closed Jan 23, 2015 at 04:26 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by maroltl · Jan 18, 2015 at 08:54 PM · returnconvertingc# to javascript

c# to js conversion

I have trouble translating this peace of code from c# to js. here is the c# code:

 public byte Block(int x, int y, int z){
   
  if( x>=worldX || x<0 || y>=worldY || y<0 || z>=worldZ || z<0){
   return (byte) 1;
  }
   
  return data[x,y,z];
 }

and here is my js code:

 function Block(x : int, y : int, z : int) : byte{
 
     if( x >= worldX || x < 0 || y >= worldY || y < 0 || z >= worldZ || z<0){
         return ???? 1;    
     }
     return data[x, y, z];
 }


I would appreciate any help or suggestions.

Comment
Add comment · Show 10
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 maroltl · Jan 19, 2015 at 04:59 PM 0
Share

I have tried anything by now and nothing works. I think that

 return data[x, y, z];

is not right either.

avatar image Eric5h5 · Jan 19, 2015 at 05:57 PM 0
Share

It is right, assu$$anonymous$$g data is of type byte[,,].

avatar image maroltl · Jan 19, 2015 at 06:02 PM 0
Share

data is of type byte[,,], but it gives me error when I play it:

NullReferenceException: Object reference not set to an instance of an object (wrapper managed-to-managed) object:ElementAddr (object,int,int,int)

avatar image Eric5h5 · Jan 19, 2015 at 06:34 PM 0
Share

You didn't initialize the array, then, so it's just null.

avatar image maroltl · Jan 19, 2015 at 06:58 PM 0
Share

How should I properly initialize it?

Show more comments

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by Eric5h5 · Jan 19, 2015 at 05:54 PM

Remove the ????, so it's just

 return 1;

It will be implicitly converted to byte, since that's what the function returns.

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 tanoshimi · Jan 19, 2015 at 05:24 PM

I'm not a Javascript person, so the following is a guess (though, in the absence of any other responses, a guess is still better than nothing, right?)

Javascript does not support multidimensional arrays. You don't include the line of code on which data is declared, but you certainly won't be able to access it as data[x,y,z]. What you can do is create an array in which each element is an array (and then each element in that array is an array, etc. etc.). In which case, you'd access a 3rd-level element as

 data[x][y][z]

As for the first problem, I believe you need to explicitly cast a variable of type byte at declaration. So, that would make the whole solution something more like:

 function Block(x : int, y : int, z : int) : byte{

   var output : byte;

   if( x >= worldX || x < 0 || y >= worldY || y < 0 || z >= worldZ || z<0){
     output = 1;
   }
   else {
     output = data[x][y][z];
   }

 return output;
 }
 

If that doesn't work, er... just use C# - it's better :)

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 Eric5h5 · Jan 19, 2015 at 05:52 PM 0
Share

Javascript (A$$anonymous$$A Unityscript) certainly does support multi-dimensional arrays.

Follow this Question

Answers Answers and Comments

27 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

Related Questions

Check if enter is pressed inside a GUI textfield 6 Answers

function that will return the name of the data type of a variable? 2 Answers

Return to a scene rather than loading again 1 Answer

C# How to ping servers and get server latency? 1 Answer

Return a custom class from function 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