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 Gilben · Oct 15, 2014 at 08:02 PM · c#prefabsarrays

Trouble instantiating an Array of Prefabs

I've been trying to create an array of prefabs based on some answers I found here, but I'm not having any luck. I keep getting a null reference error. I'm sure this is a simple problem, but I'm a noob and can't solve it. Any help would be greatly appreciated.

Here's the code i have so far:

 public GameObject[][] tiles_array;
 public GameObject tilePrefab1;

 public int boardWidth = 32;
 public int boardHeight = 32;

 float tileWidth = 5.0f;
 float tileHeight = 5.0f;



 void Start () {
 
     Debug.Log("fart");



     tilePrefab1 = Resources.Load("tilePrefab1", typeof(GameObject)) as GameObject ;      
         
         for (int i = 0; i < boardWidth; i++)
         {
             
         tiles_array[i] = new GameObject[boardWidth];
     
             for (int j = 0; j <boardHeight; j++)
             {
             tiles_array[i][j]= (GameObject)Instantiate(tilePrefab1, new Vector3(i*tileWidth,j*tileHeight,-2), Quaternion.identity);

                 
             }
         }

}

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 Eric5h5 · Oct 15, 2014 at 08:11 PM

You didn't initialize the array. Also, it would be simpler to use a 2D array instead of a jagged array...easier to initialize since you can just initialize the 2D array instead of having to initialize all of the arrays in the jagged array individually.

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 Gilben · Oct 15, 2014 at 08:53 PM 0
Share

Thank you! I switched to a 2D array and now it's at least loading. But now I have a quick follow-up, I want to create "off-set" tiles like this alt text, but I currently have thisalt text with a 2D array. I can get the desired effect using a second array offset by 1/2 the tile height/width, BUT in the future if I want to change tiles based on position (suppose a character is on top of them) I can see that making things confusing. Would a jagged array be easier for that?

Here's the current code BTW:

void Start () {

tiles_array = new GameObject[boardWidth,boardHeight];

tilePrefab1 = Resources.Load("tilePrefab1", typeof(GameObject)) as GameObject ;

for (int i = 0; i < boardWidth/2; i++) {
for (int j = 0; j

             }
         }

for (int k = 0; k < boardWidth/2; k++) {
for (int l = 0; l

         }
 }

}

capture1.png (3.2 kB)
capture2.png (21.3 kB)

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Generating a tiled world with roads, possibly using a 2D array? (C#) 0 Answers

Unity c# indexoutofrangeexception: array index is out of range 1 Answer

How to debug values in jagged arrays? 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