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
1
Question by mkjrfan · Jun 18, 2014 at 11:04 PM · arraysnull2d-array

2D Array keeps coming up as Null

Hi, I'm trying to create a 2d board in unity using a 2d array, but for some reason when trying to add an object to the 2d array it still comes out as null.

 public class BlockPlacementTest : MonoBehaviour {
 
     public GameObject floor;
 
     public int size;
     public int width, height;
 
     private int[,] goalCoords;
     private List<Room> goalRooms;
 
     private Room[,] rooms;
     
     // Use this for initialization
     void Start () {
         rooms = new Room[width,height];
         createDungeon(10);
         printArray();
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     public void createDungeon(int g){
         goalRooms = new List<Room>();
         rooms = new Room[width,height];
         goalCoords = new int[g,2];
 
         for(int i =0; i < g; i++){
             int x = (int)(Random.Range(0,rooms.GetLength(0)));
             int y = (int)(Random.Range(0,rooms.GetLength(1)));
             //Makes sure there isn't any repeats
             while(rooms[x,y] != null){
                 x = (int)(Random.Range(0,width));
                 y  = (int)(Random.Range(0,height));
             }
             goalCoords[i,0] = x;
             goalCoords[i,1] = y;
             print("X: "+x+" Y: "+y);
 //            Room r = new Room(0,size,goalCoords[i,0]*size,goalCoords[i,1]*size);
 //            r.createBlocks(true);
             goalRooms.Add(rooms[x,y] = new Room(0,size,goalCoords[i,0]*size,goalCoords[i,1]*size));
             print(rooms[x,y]);
         }
 
         connectBlocks();
 //        for( int i = 0; i < width; i++){
 //            for(int j = 0; j < height; j++){
 //                if(rooms[i,j] == null){
 //                    rooms[i,j] = new Room(1,size,i*size,j*size);
 //                }
 //            }
 //        }
     }


Room Code:

 public class Room : MonoBehaviour {
 
     public GameObject block;
 
     private int type;
     private int size;
     private int x,y;
     private int w,h;
     private int numbOfBlocks;
 
     private int[,] blocks;
 
 
     public Room(int t, int s, int x, int y){
         type = t;
         size = s;
         this.x = x;
         this.y = y;
         w = 16;
         h = w;
         blocks = new int[size,size];
         createRoom();
         createBlocks(false);
     }
 
 
     private void createRoom(){
         if(type !=1)
             for(int c = 0; c< size; c++){
                 int by = (int)(Random.Range(0,size));
                 int bx = (int)(Random.Range(0,size));
                 if(blocks[bx,by] != 1){
                     blocks[bx,by] = 1;
                 }
             }
         else
             for( int i = 0; i < size; i++)
                 for(int j = 0; j < size; j++)
                     blocks[i,j] = 1;
     }
 
     public void createBlocks(bool goal){
         for(int xb = 0; xb < blocks.GetLength(0); xb++){
             for(int yb = 0; yb < blocks.GetLength(1); yb++){
                 if(blocks[xb,yb] == 1){
 //                    Instantiate(GameObject.CreatePrimitive(PrimitiveType.Cube),new Vector3(xb+x,0,yb+y), Quaternion.identity);
                     var wall = GameObject.CreatePrimitive(PrimitiveType.Cube);
 //                    wall.AddComponent(Rigidbody);
                     wall.transform.position = new Vector3(xb+x,0,yb+y);
                     if(goal)
                         wall.renderer.material.color = new Color(0,0,0);
                     else
                         wall.renderer.material.color = new Color(1,1,1);
                 }
             }
         }
     }
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     public int[,] getBlocks(){
         return blocks;
     }
 
     public int getX(){
         return x;
     }
 
     public int getY(){
         return y;
     }
 
     public int getArrayX(){
         return x/size;
     }
 
     public int getArrayY(){
         return y/size;
     }
 
     public int getRoomType(){
         return type;
     }
 }
 

Any possible reasons why it keeps printing null?

Comment
Add comment · Show 4
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 SirCrazyNugget · Jun 18, 2014 at 11:27 PM 0
Share

Nowhere if the code you've pasted do you initialise rooms[], without seeing where you've set rooms[] it's expected to return null.

avatar image mkjrfan · Jun 18, 2014 at 11:34 PM 0
Share

I initialized it in the start() method

avatar image RyanPaterson · Jun 19, 2014 at 12:10 AM 0
Share

Post all relevant code, otherwise we might not be able to help (include your Room script)

avatar image mkjrfan · Jun 19, 2014 at 12:37 AM 0
Share

Okay I updated it with the room code

1 Reply

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

Answer by Bunny83 · Jun 19, 2014 at 12:53 AM

Well, the answer is simple:

You created a MonoBehaviour with "new"!

You can't create MonoBehaviours (or any kind of Component) manually. You always have to use AddComponent. Components can only "live" on GameObjects. If you create a MonoBehaviour with new, it's missing it's native part and it "fakes" itself to be null. The same happens when you destroy a component. As you might know in managed environments you usually can't destroy objects. Objects are garbage collected when all references to that object are gone.

However components can be destroyed (they are actually C++ objects, the C# class is "just" the managed counterpart). When you Destroy() a component (or any other object derived from UnityEngine.Object) the native part get destroyed but the managed part will be there until it's garbage collected once all references are gone. The managed object has an internal flag that it's actually dead. The UnityEngine.Object has overridden the Equals and ==operator to fake the reference is null.

You shouldn't implement a constructor on MonoBehaviour classes. Instead you can implement a create method like this:

 public static Room Create(int t, int s, int x, int y)
 {
     GameObject GO = new GameObject("Room_"+x+"_"+y);
     Room R = GO.Addcomponent<Room>();
     R.type = t;
     R.size = s;
     R.x = x;
     R.y = y;
     R.w = 16;
     R.h = w;
     R.createRoom();
     R.createBlocks(false);
     return R;
 }

this method will create a new GameObject with a Room script attached, will initialize your desired variables and return the reference to the Room component.

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 mkjrfan · Jun 19, 2014 at 01:09 AM 0
Share

Okay thanks, but I do have one question. How can I use the addComponent with the array and the list? Edit: Never $$anonymous$$d it works. Thank you again!

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

23 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

Related Questions

Object is null when it shouldn't be. 1 Answer

How to remove null's from 2d array ? It's removing but only 2 out of 4 null's. 0 Answers

Calling prefabs by name or tag returns null. 1 Answer

arrays are giving me null reference exceptions and I don't know why 1 Answer

Function like string.IsNullOrEmpty(var) but for arrays 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