• 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
Question by ugotstoopt · Feb 14, 2021 at 08:22 AM · instantiatearrayspawning problemsjagged

Using a jagged array to spawn a GameObject

I am trying to spawn enemy GameObjects onto pre-defined spawn points. The spawn points are set up as a 3 x 2 grid using a jagged array. When the game starts, it should spawn a random number of enemies (from 1 - 6) using the transform.position of the values within the jagged array, but it doesn't seem to be working. Here's the code of my game manager object (battleManager):

     public class battleManager : MonoBehaviour
     {
             public GameObject[] team1; 
             public GameObject[] team2;
         
             public GameObject[] enemyUnits = new GameObject[6];
         
             public GameObject team1Grid;
             public GameObject team2Grid;
             
             private int numberOfEnemies;
             private int chooseEnemy;
             private int chooseSpawnRow;
             private int chooseSpawnPoint;
         
         void Start()
             {
                 team1Grid.GetComponent<battleGrid>().xStartPos = 57;
                 team1Grid.GetComponent<battleGrid>().zStartPos = 35;
                 Instantiate(team1Grid);
         
                 team2Grid.GetComponent<battleGrid>().xStartPos = 57;
                 team2Grid.GetComponent<battleGrid>().zStartPos = 40;
                 Instantiate(team2Grid);
         
                 SpawnEnemies();
             }
         
         
         public void SpawnEnemies()
                 {
                     numberOfEnemies = Random.Range(1, 6);
             
                     team2 = new GameObject[numberOfEnemies];
             
             
                     for (int i = 0; i < team2.Length; i++)
                     {
                         chooseEnemy = Random.Range(0, enemyUnits.Length);
             
                         chooseSpawnRow = Random.Range(0, 1);
                         chooseSpawnPoint = Random.Range(0, 2);
             
                         team2[i] = Instantiate(enemyUnits[chooseEnemy], team2Grid.GetComponent<battleGrid>().grid[chooseSpawnPoint][chooseSpawnRow].transform.position, Quaternion.identity);
                     }
             
                 }
 }

And here's the code for the battleGrid object:

 public class battleGrid : MonoBehaviour
 {
     public GameObject spawnPosition;
 
     public int xStartPos; // Where the grid begins 
     public int zStartPos; // Where the grid begins
     public float xSpace = 3; // Spacing between spawn point tiles
     public float zSpace = 2; // Spacing between spawn point tiles   
 
     static int rowLength = 3;
 
     public GameObject[] frontRow = new GameObject[rowLength];
     public GameObject[] backRow = new GameObject[rowLength];
 
     public GameObject[][] grid = new GameObject[2][];
 
     void Start()
     {
 
         for (int i = 0; i < frontRow.Length; i++)
         {
             frontRow[i] = spawnPosition;
         }
 
         for (int i = 0; i < backRow.Length; i++)
         {
             backRow[i] = spawnPosition;
         }
 
         grid[0] = frontRow;
         grid[1] = backRow;
 
         for (int i = 0; i < frontRow.Length; i++)
         {
             for (int j = 0; j < grid.Length; j++)
             {
                 grid[j][i] = Instantiate(grid[j][i], new Vector3(xStartPos + (xSpace * i), 0, zStartPos + (zSpace * j)), Quaternion.identity);
                 Debug.Log(grid[j][i].transform.position);
             }
         }
     }

In the first script (battleManager), I can't instantiate using the transform.position of the jagged array elements ("grid[][]"). However each element does seem to have a transform.position value when instantiated in the battleGrid object as I use Debug.Log to verify.

Can someone clarify how I can access the transform.position values of jagged array elements to instantiate Game Objects? Thank you

Comment

People who like this

0 Show 0
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
Best Answer

Answer by ugotstoopt · Feb 14, 2021 at 07:55 PM

Fixed it, in case it helps anyone in the future the answer is I had to change Instantiate(team1Grid);to team1Grid = Instantiate(team1Grid);, and do the same for the 2nd grid.

Comment

People who like this

0 Show 0 · 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.

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

190 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 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

[SOLVED] Remove Vector3 Array step by step when GameObject spawned 1 Answer

Can I spawn with an array in order? 2 Answers

Instantiate prefabs at different position 2 Answers

Instantiate isnt working for spawner class 1 Answer

Multiple Instantiations not working 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