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 Rabur · Oct 22, 2015 at 01:34 PM · instantiatearray of gameobjectsinstantiating

instantiating into array

Hello,

Straight to the topic:

Is there any way to instantiate objects straight into the array of game objects ?

 using UnityEngine;
 using System.Collections;
 
 public class Background : MonoBehaviour {
     public GameObject [][] background;
     public GameObject cube; 
     private float transform_x ;
     private float transform_z ;
     
     // Use this for initialization
     void Start () 
     {
         transform_x = -6f;
         transform_z = -11f;
         for( int x = 0; x < 7; x ++)
         {
             for(int y = 0; y < 12; y ++)
             {
                 Instantiate(cube, new Vector3 ( transform_x, 0, transform_z), Quaternion.Euler(90,0,90));
 
                 transform_z =transform_z + 2;
                 if(transform_z == 13)
                 {
                     transform_z = -11;
                 }
             }
             transform_x = transform_x +  2;
         }
         
     }
     
     
     
 }
 

I've got this so far but i don't really know how to throw this instantiated game object into the array.

any hints? :F

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

2 Replies

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

Answer by Statement · Oct 22, 2015 at 02:32 PM

First you need to create the array (unless it has been created elsewhere).
Then you can set values to it like so array[x][y] = value;

What you have is called a jagged array. They are usually used when each row have different lengths. In your case you could use a two-dimensional array.

 // Jagged array
 public class Background : MonoBehaviour
 {
     public int backgroundWidth = 7;
     public int backgroundHeight = 12;
     public GameObject[][] background;
     public GameObject cube;
 
     private float transform_x;
     private float transform_z;
 
     void Start()
     {
         transform_x = -6f;
         transform_z = -11f;
 
         background = new GameObject[backgroundWidth][];
         for (int x = 0; x < backgroundWidth; x++)
         {
             background[x] = new GameObject[backgroundHeight];
             for (int y = 0; y < backgroundHeight; y++)
             {
                 Vector3 position = new Vector3(transform_x, 0, transform_z);
                 Quaternion rotation = Quaternion.Euler(90, 0, 90);
                 background[x][y] = (GameObject)Instantiate(cube, position, rotation);
 
                 transform_z = transform_z + 2;
                 if (transform_z == 13)
                 {
                     transform_z = -11;
                 }
             }
             transform_x = transform_x + 2;
         }
     }
 }

And an example with a 2d array.

 // Two-dimensional array
 public class Background : MonoBehaviour
 {
     public int backgroundWidth = 7;
     public int backgroundHeight = 12;
     public GameObject[,] background;
     public GameObject cube;
 
     private float transform_x;
     private float transform_z;
 
     void Start()
     {
         transform_x = -6f;
         transform_z = -11f;
 
         background = new GameObject[backgroundWidth, backgroundHeight];
         for (int x = 0; x < backgroundWidth; x++)
         {
             for (int y = 0; y < backgroundHeight; y++)
             {
                 Vector3 position = new Vector3(transform_x, 0, transform_z);
                 Quaternion rotation = Quaternion.Euler(90, 0, 90);
                 background[x, y] = (GameObject)Instantiate(cube, position, rotation);
 
                 transform_z = transform_z + 2;
                 if (transform_z == 13)
                 {
                     transform_z = -11;
                 }
             }
             transform_x = transform_x + 2;
         }
     }
 }


Comment
Add comment · Show 2 · 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 Statement · Oct 22, 2015 at 02:39 PM 0
Share

Also, Instantiate(Object, Vector3, Quaternion) return an Object type and there is no template overload, so you have to cast the result to GameObject. See the example above.

avatar image Rabur · Oct 23, 2015 at 04:39 PM 0
Share

Thanks a lot!

avatar image
0

Answer by exltus · Oct 22, 2015 at 02:02 PM

If I understand your question right, that this might help.

 background[x][y] = Instantiate(cube, new Vector3 ( transform_x, 0, transform_z), Quaternion.Euler(90,0,90)) as GameObject;
Comment
Add comment · 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

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

33 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

Related Questions

Instantiate prefabs at different position 2 Answers

I'm currently trying to make an inventory system and not sure what the best way is to display an items info 0 Answers

instantiate from two seperate values in for loop 1 Answer

How to instantiate an array of prefabs into an array of empty gameobject positions 0 Answers

I am trying to access transform[] and count from SCRIPT A, however, its not working perfectly. SCRIPT B didnt apply the effect onto the instantiated fish from SCRIPT A. Only some fishes fly to the targets. 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