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 /
avatar image
0
Question by ExtinctChicken · Jul 03, 2017 at 10:39 PM · instantiatearrayclassfor-loop

Can't assign an instantiated class to an array?

I use a for loop to instantiate a transform for each element in a Grid array; then I assign each element in the Grid array like such:

 public Grid[] board;

 for(int i = 0; i < board.Length; i++)
 {
             Transform newGridParent = Instantiate(gridParentObject, startingPos, 
             transform.rotation);
             newGridParent.SetParent(this.transform);
 
             board[i] = newGridParent.GetComponent<Grid>();
             board[i] = new Grid(rows, startingPos, gridChildObject, 
             newGridParent);
 
             startingPos += new Vector3(10, 0, 0);    
 }

I first instantiate a transform which has the class component, then assign that component from that transform to the array here:

 Transform newGridParent = Instantiate(gridParentObject, startingPos, transform.rotation);
 board[i] = newGridParent.GetComponent<Grid>();

But when I run the game, the board array is empty, even though I clearly assigned each element in board? What is the error here??

Here's the Grid Class:

 [System.Serializable]
 public class Grid : MonoBehaviour {
 
     public Transform[] grid;
     bool visited;
 
     public Grid (int count, Vector3 startPos, Transform gridObj, Transform parent)
     {
         grid = new Transform[count];
 
         for(int i = 0; i < grid.Length; i++)
         {
             Transform newGridChild = Instantiate(gridObj, startPos, Quaternion.Euler(new Vector3(-90, 0, 0)));
             newGridChild.SetParent(parent);
             startPos += new Vector3(0, 10, 0);
         }
     }
 }

Thanks

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
1
Best Answer

Answer by bobisgod234 · Jul 04, 2017 at 12:22 AM

  public Grid[] board;
  for(int i = 0; i < board.Length; i++)

This will only work if the board array already has elements in it (including null). Did you set the lenth of the 'board' array in the inspector beforehand? If you haven't, this won't do anything, since board.Length will be 0. Add a Debug.Log statement into for for loop to verify if its running or not.

              board[i] = newGridParent.GetComponent<Grid>();
              board[i] = new Grid(rows, startingPos, gridChildObject, 
              newGridParent);

You are assigning the Grid component attached to the instance of your "gridParentObject", but then you are overriding that with a new grid anyway, so you will never reference the Grid attached to the instanced transform. This can't be intentional.

      public Grid (int count, Vector3 startPos, Transform gridObj, Transform parent)
      {
 }

You cannot use constructors with Monobehaviours, this script should be producing an error in the console when you try to create a new Grid. Instead, either do your initialization in the Awake/Start function, or alternatively add a public Setup function that takes the same arguments as your constructor, and call that.

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 ExtinctChicken · Jul 04, 2017 at 02:17 AM 0
Share

I see. I replaced the constructor and did the initialization differently and it works fine for now. Thanks :)

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

87 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

Related Questions

Instantiate object in for loop with array 2 Answers

Instantiating gameobjects in an array / class problem | NullReferenceException: 0 Answers

How can I instantiate a object with a constructor? 3 Answers

adding "X" amount of objects to an array 2 Answers

Object reference not set to an instance of an object with object set, 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