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 Mr0w3m · Mar 06, 2016 at 11:15 PM · c#arraysarray of gameobjectsnull reference exception

Cannot access 2D array element

alt text

Edit2 ^^ That is the code I have, for some reason the editor here is deleting parts of it.

So I've spent a significant amount of time on this issue and I'm wondering if it is a bug or if I'm just flippin nuts.

Okay so I have a 2D array of GameObject tiles with an x of 10 and y of 12. I have the array on a GameManager script on a separate GameObject in the scene. When the game begins a circle is instantiated as a child of one of the gameObjects in the array, thus putting it on that 'space'. The deal is I'm trying to access the GameManager script from the circle's script through a public GameObject variable. This variable is set in the inspector so I should just be able to access the GameManager Script right?

The code I have to access the script is:

public GameManager gameManager;

TileInfo tileInfo = gameManager.tile[topLeftX, topLeftY].transform.GetComponent();

//This always gets a null reference exception.

I'm trying to access the 2D array from the gameManager.

Ultimately I'm looking to: grab a script from the tile, through the gameManager's 2Darray, through a public GameObject in the script that is above.

I'm sorry if this is confusing, I'll put more if you think I need it, I've debugged down to that accessing the gameManager script is the problem.

Thanks!

cap001.jpg (19.3 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by EmHuynh · Mar 06, 2016 at 11:18 PM

Hello, @Mr0w3m.

Edit: Maybe, the variable gameManager is null and not the array, since they've been instantiated. Unless, they were destroyed.

When calling, the GetComponent function, you need to set its parameter "type" to the type of the component you are trying to reference to. In this case, its TileInfo. Here is the documentation: Component.GetComponent.

TileInfo tileInfo = gameManager.tile[ topLeftX, topLeftY ].GetComponent< TileInfo >();

Comment
Add comment · Show 4 · 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 Mr0w3m · Mar 06, 2016 at 11:38 PM 0
Share

I actually typed ();> but it keeps deleting that part in <> because it comes up with a red underline, I don't know how to fix this. Edit1: whatever spellcheck is set up here is deleting parts of my code...

avatar image EmHuynh · Mar 07, 2016 at 08:03 AM 0
Share

Hello, @$$anonymous$$r0w3m.

Okay, I understand. Thanks for the clarity and for providing an image. $$anonymous$$y answer has been improved. TileInfo tileInfo = game$$anonymous$$anager.GetComponent< TileInfo >().tile[ topLeftX, topLeftY ];

avatar image Mr0w3m EmHuynh · Mar 07, 2016 at 05:37 PM 0
Share

Thank you for the reply, but I'm trying to access the 2D array on the game$$anonymous$$anager, and then access the TileInfo component of a part of the 2D array, not the other way around. I've updated my code so it makes more sense and posted a better structured question herelink text

avatar image EmHuynh Mr0w3m · Mar 08, 2016 at 08:14 PM 0
Share

Hey, @$$anonymous$$r0w3m. Did you initialize the variable, game$$anonymous$$anager?

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

127 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 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 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 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 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 state of variable for each item in an Array 1 Answer

Having a issue with a NullReferenceException 1 Answer

NullReferenceException with XML Deserializer 0 Answers

Array of Arrays 3 Answers

Unity Bug? null reference exception, Only its not null... 0 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