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 Nkmol1 · Feb 09, 2015 at 12:16 PM · c#initialization

Init code goes to Null unknow reason

Hello Unity Community,

This is very basic question and i feel ashamed to ask this, but i feel i'm missing something. I'm simply initializing my grid on Start(), however when i try to acces it from addBuilding() it returns null.

Start() is the only place where the grid is assigned. It seems so simpel, but because it is so simple quite hard.

 public int rectangles_x, rectangles_z;
 private GameObject[,] grid
 void Start () {
     grid = new GameObject[rectangles_z , rectangles_x];
     // A debug log returns the right 2D array
 }
 
 public void addBuilding(int x, int z, GameObject obj) {
    Debug.Log (this.grid); // This returns Null
    //grid [z, x] = obj; //This part gives "NullReferenceException" because grid == null
 }


Hopefully anyone can help me.

With kind regards, Nkmol

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 HarshadK · Feb 09, 2015 at 12:08 PM 0
Share

Read about multidimensional arrays to understand how they are initialized and populated.

What are rectangles_z and rectangles_x?

avatar image Nkmol1 · Feb 09, 2015 at 12:37 PM 0
Share

@HarsHad$$anonymous$$ I don't think my syntax is wrong? rectangles_z and rectangles_x are variables assigned through unity GUI.

2 Replies

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

Answer by Nkmol1 · Feb 09, 2015 at 06:39 PM

it was my Script Excution Order. Apperently Start() wasn't called first. I feel so stupid ^^ Thanks for the help all!

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 meat5000 ♦ · Feb 09, 2015 at 06:44 PM 0
Share

Accept your answer, click the tick

avatar image
0

Answer by stblue · Feb 09, 2015 at 01:34 PM

Its returning null because you have just supply the dimension and by default it will get initialize to "null" and since its not pointing to any objects right now, which means its value is null.

Comment
Add comment · Show 8 · 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 Nkmol1 · Feb 09, 2015 at 01:38 PM 0
Share

This is what i suspected. Howerver i thought C# supported null as objects. Like GameObject obj = null; is also possible, right?

Anyhow, how can i make that the empty elements are represented as null?

avatar image stblue · Feb 09, 2015 at 02:48 PM 0
Share

"null" simply means it doesn't point to any location. You can definitely use GameObject obj = null. You can also see it like GameObject "obj" is a basket and by default it doesnot contain anything which means null or empty. When you use something like obj = gameobjectAAA(suppose), it start to pointing gameobjectAAA,which means you have put gameobjectAAA in you empty basket(Gameobject "obj" ). Hope you understand.

avatar image meat5000 ♦ · Feb 09, 2015 at 02:51 PM 0
Share

It usually makes sense to catch null cases. The simplest way is to check with if statement before you perform your operation.

avatar image Nkmol1 · Feb 09, 2015 at 03:16 PM 0
Share

@stblue I know how Objects work and about their instances. Please read the question above. Because you say it is possible, though why doesn't it work in the case i am trying?

I simply check if null then i know it is empty.

avatar image saschandroid · Feb 09, 2015 at 04:12 PM 1
Share

I tested with

 GameObject[,] grid;
 grid = new GameObject[10, 5];
 Debug.Log("1 Grid: " + grid);
 grid[1, 2] = new GameObject();
 Debug.Log("2 Grid: " + grid);

and it does not return null (both Debug.Logs). Are you sure Start() is entered and the grid initialized before calling addBuilding?

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Initialising List array for use in a custom Editor 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Does Unity support application bootstrapping? 1 Answer

Is OnEnable fired right after Awake? 2 Answers


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