• 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 MarvinH · Sep 10, 2015 at 06:27 PM · gameobjects

How to Instantiate through a list of Gameobjects?

Hello,

I am trying to build a list of gameobjects based on the units the player selects. Right now, when the player clicks a button, that unit is added to the list. When the player hits 'Go' all objects in that list are instantiated. What currently happens is that every element but the LAST 2 are instantiated. What am I doing wrong? How do i properly step through a list of objects?

Here is my code. Let me know if I need to clarify this.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class UnitManager : MonoBehaviour {
 
     public GameObject[] units;  //Array of unit gameObjects
     public GameObject[] wave; //The current wavew of the Player's units
     public GameObject[] pathPoints; //List of Path points
 
     //Class Objects
     /*****************************************************/
     public int currentnum =0;
     private Spawner Spawner;
     private GameManager gameControl;
     public GameObject spawnpoint;
     private unitBase unitBase;
 
     private unitDatabase database;  //Database of units
     public List<unitBase> _units;
     public List<unitBase> _waves;
 
     public Queue< string > buildQueue;
 
     /******************************************************/
 
     //other variables
     public float maxBuildProgress =10;
     private float currentBuildProgress = 0.0f;
 
     private int index = 0;
 
     // Use this for initialization
     void Start () {
     
         //Add the qeue for building 
 
         buildQueue = new Queue<string>();
 
         Spawner = GameObject.FindGameObjectWithTag("Spawner").GetComponent<Spawner>();
         gameControl = GameObject.FindGameObjectWithTag("Manager").GetComponent<GameManager>();
         database = GameObject.FindGameObjectWithTag("Unit Database").GetComponent<unitDatabase>(); //Controls the units available
         _units = new List<unitBase>(); 
         _waves = new List<unitBase>(); 
 
     }
               
 
     void OnGUI(){
 
         for(int i =0; i < units.Length; i++)
         {
             if(GUI.Button(new Rect(Screen.width/20, Screen.height/12*i, 200, 30),"Build  " + _units[i].unitName)) //Did player click unitt button
             {
                 //First check the wave capacity
                 //If the unit will not exceed the capacity of the wave, Check the money
 
                     if(gameControl.unitCount <=  gameControl.waveCapacity)
                     {
                         if(gameControl.PlayerMoney >= _units[i].unitCost)
                         {
                         gameControl.PlayerMoney -= _units[i].unitCost;
                         //sPAWNER handles the creation of the base mesh/gameobject
 
                         //Add units to list
                         _waves.Add(_units[i]);
                         //Add the unit to the map as agame object
 
                         gameControl.unitCount += _units[i].unitSupply;
                         //Spawner.Spawn(units[i]); //Spawn the unit that the player clicks on (I)
 
                         //Need to only instantiate on button press
 
                     }
                     }
                 }
 
         
             }
 
         //Instantiate  the unit in the wave
 
         if(GUI.Button(new Rect(600, Screen.height/12, 200, 30),"Go"))
         {
 
             BuildStuff();
 
             //Reset the Wave back to 0
         
             }
 
 
         }
 
     void BuildStuff()
     {
         for(int j= 0 ; j <= _waves.Count; j++)
         {
             
             
             GameObject newUnit = ((GameObject)Instantiate(Resources.Load(_waves[j].unitName), spawnpoint.transform.position, spawnpoint.transform.rotation)) as GameObject;
             //Need to iterate
             _waves.Remove(_waves[j]); //Remove the units in wave
             
             newUnit.SendMessage("SetPathPoints", pathPoints);
             
         }
 
         _waves[0]= 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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

sync color change of objects c# 1 Answer

toggle gameObjects with a keyPress 1 Answer

What is the Performance implications of stacking gameobjects and switch between them for character customization. 1 Answer

Combine two or more gameobjects with different rigidbody values 0 Answers

How do I check if a boolean variable is true on a group of gameobjects with the same script? 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