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 CascadiaLeijona · Sep 04, 2015 at 04:52 PM · javascriptarraysmatrixcasting

InvalidCastException

Hello! I'm having trouble translating the C# from the Cellular Automata tutorial into Javascript. I've built a constructor inside my function that creates a nested array variable called matrix. Then I have a series of for loops that first assign each cell in the nested array a number value (1 or 0) and then draw a cube in a location in the game world based on the corresponding cell's value and location in the nested array.

Unfortunately, Unity seems to be having issues with the casting of my nested array variable.

Here's my code. // Declaring/typing variables var w: int; var h: int; var x: int; var y: int; var fill: float;

 function Update() {
     if (Input.GetMouseButtonDown(0)) {
         GenerateMap(w, h);
     };
 };
 
 function GenerateMap(width, height) {
     var matrix = new array(width);
     for (x = 0; x < width; x++) {
         matrix[x] = new array(height);
     };
     for (x = 1; x < width - 1 ; x++) {
         for (y = 1; y < height - 1; y++) {
             if (x == 1 || x == width - 1 || y == 1 || y == height - 1) {
                  matrix[x][y] = 1;
             };
             else if (Random.value < fill.value) {
                 matrix[x][y] = 0;
             };
             else {
                 matrix[x][y] = 1;
             };
         };
     };
     for (x = 1; x < width - 1; x++) {
         for (y = 1; y < height - 1; y++) {
             if (matrix[x][y] == 1) {
                 Gizmos.color = Color.black;
             };
             else {
                 Gizmos.color = Color.white;
             };
             var pos = new Vector3();
             Gizmos.DrawCube(Vector3.one, Vector3.one);
         };
     };
 };

The error comes at line 15, where Unity gets the matrix variable. The exact error is

InvalidCastException: Cannot cast from source type to destination type. CaveGenerator.GenerateMap (System.Object width, System.Object height) (at Assets/Scripts/CaveGenerator.js:15) CaveGenerator.Update () (at Assets/Scripts/CaveGenerator.js:10)

I would really appreciate any insight into the problem here.

Comment
Add comment · Show 1
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 getyour411 · Sep 05, 2015 at 05:48 AM 0
Share

Are 8 and 10 right? I don't know UnityScript but the matrix 2-dim array definition there (twice?) looks odd.

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

Sort an array in reverse alphabetical order 2 Answers

Is it possible to summon specific GameObjects (i.e. from Assets) on a specific position in a jagged array so that I can later Destroy said object? 0 Answers

Why i can't read a Variable's Length(Array) 2 Answers

Can I use an array to determine an enclosed area and remove objects inside? 0 Answers

how to not repeat random array 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