• 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 Williamsii · Apr 06, 2016 at 10:51 PM · arraysreferencesrun-time

How to access objects by reference stored in array?

Hello! I dynamically create (during run-time) a set of objects and supposedly store references to them in a two-dimensional array. When I try to get one of stored object through a public method (by call from another script if it matters) I get NULL. Perhaps I'm missing something about scopes or what the heck is going on? Field property contains prefab.

 public class Field : MonoBehaviour {
       public GameObject field;
       private GameObject[,] FieldSet = new GameObject[5,5];
     
       void Start () {
         for (int _i = 0; _i < 5; _i++)
         {
           for (int _j = 0; _j < 5; _j++)
           {
             FieldSet[_i, _j] = Instantiate(field);
             print(FieldSet[_i, _j]);  // prints "field clone"
           }
         }    
       }
     
       public void ShowField(int a, int b){      //a = 1, b = 2
          print(FieldSet[a,b]);  //    prints "NULL"
       }
     }
 
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
Best Answer

Answer by TBruce · Apr 06, 2016 at 11:52 PM

@Williamsii Your script works fine. I added an Update method to the class and iterated through all game objects in the field set printing out their name (using Debug.Log). Are you sure that "public GameObject field" is initialized?

 public GameObject field;
 private GameObject[,] FieldSet = new GameObject[5,5];
 private bool initialized = false;

 void Start ()
 {
     for (int _i = 0; _i < 5; _i++)
     {
         for (int _j = 0; _j < 5; _j++)
         {
             FieldSet[_i, _j] = Instantiate(field);
         }
     }    
 }

 public void Update()
 {
     if (!initialized)
     {
         initialized = true;
         for (int _i = 0; _i < 5; _i++)
         {
             for (int _j = 0; _j < 5; _j++)
             {
                 ShowField(_i, _j);
             }
         }    
     }    
 }
 
 public void ShowField(int a, int b)
 {
     if (FieldSet[a, b] != null)
     {
         Debug.Log("Field[" + a + "][" + b + "].name = " + FieldSet[a, b].name);
     }
     else
     {
         Debug.Log("Field[" + a + "][" + b + "] = " + null);
     }
 }
 
 Field[0][0].name = Field(Clone)
 Field[0][1].name = Field(Clone)
 Field[0][2].name = Field(Clone)
 Field[0][3].name = Field(Clone)
 Field[0][4].name = Field(Clone)
 Field[1][0].name = Field(Clone)
 Field[1][1].name = Field(Clone)
 Field[1][2].name = Field(Clone)
 Field[1][3].name = Field(Clone)
 Field[1][4].name = Field(Clone)
 Field[2][0].name = Field(Clone)
 Field[2][1].name = Field(Clone)
 Field[2][2].name = Field(Clone)
 Field[2][3].name = Field(Clone)
 Field[2][4].name = Field(Clone)
 Field[3][0].name = Field(Clone)
 Field[3][1].name = Field(Clone)
 Field[3][2].name = Field(Clone)
 Field[3][3].name = Field(Clone)
 Field[3][4].name = Field(Clone)
 Field[4][0].name = Field(Clone)
 Field[4][1].name = Field(Clone)
 Field[4][2].name = Field(Clone)
 Field[4][3].name = Field(Clone)
 Field[4][4].name = Field(Clone)
 
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 Williamsii · Apr 07, 2016 at 12:16 AM 0
Share

Hello $$anonymous$$avina! thanks for reply. Yes, it seems that calling this method from inside the same class is successful. But my intention was to use it as public method and thats where it fails - on public. got any ideas why?

avatar image TBruce Williamsii · Apr 07, 2016 at 12:42 AM 0
Share

Then do this:

[HideInInspector] public GameObject[,] FieldSet = new GameObject[5,5];

avatar image Williamsii Williamsii · Apr 07, 2016 at 08:43 PM 0
Share

my mistake was to create new instance of field class, therefore i couldn't get array. stupid me)

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

55 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

Related Questions

C# ArrayList Accessing and RemoveAt? 0 Answers

Another Physics.OverlapSphere question 0 Answers

Find all plants and add their x,y,z in arrays not working. [Solved] 0 Answers

Button locking/unlocking for loop using an object array & playerprefs 1 Answer

Assigning a different material to different objects 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