• 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 Kroltan · Nov 04, 2011 at 02:20 PM · matrixcubes

Basic terrain generator

I am trying to generate a array of blocks. This is my code:

MapGenerator.js

 class MapGenerator extends MonoBehaviour {
 function Start(){
 Debug.Log("HERP");
     gameMatrix = MakeVoxels(128, 128, 128);
 }
 function MakeVoxels(sizeX:int, sizeY:int, sizeZ:int){
     var matrix : Block[];
     var currX:int;
     var currY:int;
     var currZ:int;
     for (currZ = 0-(currZ/2); currZ<=sizeZ; currZ++){
         for (currY = 0-(currY/2); currY<=sizeY; currY++){
             for (currX = 0-(currX/2); currX<=sizeX; currX++){
                 matrix[(currZ*sizeZ)+(currY*sizeY)+currX] = new Block(BlockMaterial.AIR,currX, currY, currZ);
             }
         }
     }
     Debug.Log("DERP");
     return matrix;
 }

}

Block.js

 class Block{

 var breakTime:float;

 var color:Color;

 public var blockMat:Material;

 function Block(mat:BlockMaterial, x:int, y:int, z:int){

     var cube:GameObject;

     if (mat == BlockMaterial.AIR){

         cube = SetBlockProps(x, y, z, new Color(255,255,255,1), false);

     }else if (mat == BlockMaterial.DIRT){

         cube = SetBlockProps(x, y, z, new Color(84,59,20,255), true);

     }else if (mat == BlockMaterial.STONE){

         cube = SetBlockProps(x, y, z, new Color(111,111,111,255), true);

     }

 }

 function SetBlockProps(x:int, y:int, z:int, col:Color, solid:boolean){

     var c:GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);

     c.transform.position = new Vector3(x,y,z);

     c.renderer.material = blockMat;

     c.renderer.material.color = col;

     c.collider.enabled = solid;

     return c;

 }

}

BlockType.js

 enum BlockMaterial{

 AIR,

 DIRT,

 STONE

}

I want to generate a matrix of cubes on MapGenerator.js, but it errors on line

 matrix[(currZ*sizeZ)+(currY*sizeY)+currX] = new Block(BlockMaterial.AIR,currX, currY, currZ);

This is the error: NullReferenceException: Object reference not set to an instance of an object (wrapper stelemref) object:stelemref (object,intptr,object) MapGenerator.MakeVoxels (Int32 sizeX, Int32 sizeY, Int32 sizeZ) (at Assets/Scripts/MapGenerator.js:14) MapGenerator.Start () (at Assets/Scripts/MapGenerator.js:4)

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 Owen-Reynolds · Nov 04, 2011 at 04:09 PM 0
Share

I think the math on the last line is wrong. Suppose you have a 6x4x5 grid. Each +1Y should add 5 (to go to next x-row.) Each x,y plane is 24 cubes, so +1Z should add 24. Your math gives the end corner (5,3,4) only: 4*5+3*4+5=37 -- much less than then 120 total.

Should be matrix[curZ*sizeX*sizeY + curY*sizeX + sizeX]

1 Reply

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

Answer by syclamoth · Nov 04, 2011 at 02:37 PM

The problem is that while you declare matrix in your script, you never tell it how long it's supposed to be! You should initialise matrix with the total length it is expected do end up, something like this-

 var matrix : Block[] = new Block[sizeX * sizeY * sizeZ];

Another thing, you never actually define a return type for that function? I'm not quite sure about the syntax, but isn't that a problem? Or is it another one of those things where JavaScript's weird fuzzy syntax magically makes it work for you?

Comment
Add comment · Show 3 · 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 DaveA · Nov 04, 2011 at 02:55 PM 0
Share

Javascript lets you do that, but if you want it on mobile, best to be specific.

avatar image DaveA · Nov 04, 2011 at 02:57 PM 0
Share

You may want a multidimensional array. I've not tested but maybe: var matrix : Block[,,]; matrix = new Block[sizeX,sizeY,sizeZ];

avatar image Kroltan · Nov 04, 2011 at 07:21 PM 0
Share

Thanks! It worked!

About the return type: yes, that is JS' syntax freedom.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

GUI.Matrix Problems 0 Answers

Surface Shader with Material Matrix 1 Answer

How can I transform a primitive using only GL and matrix multiplication (no GameObject)? 1 Answer

CG - Using Matrix as texcoord 1 Answer

Use methods of Transform without linked gameObject 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