Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Storing Instantiated into an Array?

Discussion in 'Scripting' started by DanSingh, Jun 23, 2016.

  1. DanSingh

    DanSingh

    Joined:
    Feb 5, 2015
    Posts:
    98
    I am currently working on a script that would only Instantiate about 5 gameobjects from a prefab, however,
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ObjectSpawns : MonoBehaviour
    5. {
    6.     public GameObject healthPack;
    7.     public Transform [] healthPackSpawns;
    8.     int temp = 0;
    9.     public GameObject [] healthPackClones;
    10.  
    11.  
    12.     // Use this for initialization
    13.     void Start ()
    14.     {
    15.         InvokeRepeating("SpawnHealthPacks",3f,3f);
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update ()
    20.     {
    21.     }
    22.  
    23.     void SpawnHealthPacks()
    24.     {
    25.         healthPackClones[temp] = Instantiate(healthPack,healthPackSpawns[0].transform.position,healthPackSpawns[0].transform.rotation) as GameObject;
    26.         temp++;
    27.        
    28.     }
    29. }
    30.  
    I would like to manipulate the spawned objects later on for destroyed uses, I thought I might just store it in an array but I don't understand how with the Instantiate method, I know I could do a search with the name of the object + "(clone)" but I feel like this is too demanding then rather storing the objects in memory, but I just want to store it in an array as soon as it spawns, can you please help, thanks. I have the code below.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    If you want to add stuff during the game, it may be better to use a list. An Array has a predefined size that doesn't change (unless you create a new array to change the size). If you want to use the array, that is fine also. Just make sure you initialize it to the size you want it to be.

    Next, right now you have no exit. InvokeRepeating runs forever, so you'll keep creating healthpacks. Not sure if that was your desire, but make sure you stop the invoke after you've spawned the required healthpacks.

    As far as getting the healthpacks, you can just access the index in your List or Array.

    Code (CSharp):
    1. List<GameObject> healthPackClones = new List<GameObject>();
    2.  
    3. healthPackClones.add(Instantiate(healthPack,healthPackSpawns[0].transform.position,healthPackSpawns[0].transform.rotation) as GameObject);
    4.  
    5. healthPackClones[0].setActive(false); //This sets the first healthpack inactive. And works on either arrays or List.
    If you want them to spawn in certain locations, you can replace healthPackSpawns[0] with healthPackSpawns[healthPackClones.Count] if you have spawn points set up.
     
    Last edited: Jun 23, 2016
  3. DanSingh

    DanSingh

    Joined:
    Feb 5, 2015
    Posts:
    98
    I get it, but I didn't think an array list would work, but it makes sense,and the invoke repeating was a way for me to test the spawn without spamming helathpack for like every frame. But when I did it my way I forgot to say the health packs wouldn't store in the array I haven't tried it your way but I hope it works. Thanks
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    not an "array list", a "list". They are different.
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    @LeftyRighty: Thank you. I could see the confusion.

    @DanSingh: If your array doesn't have room, it's not going to be able to add your healthPacks to it, which is why I suggested the List<GameObject> instead. List can expand, however you have to be careful depending on what you are doing. If you remove an object from the list, the others adjust down. (If you remove index 2, then index 3 moves into 2's slot).

    Arrays don't adjust that way.
     
  6. DanSingh

    DanSingh

    Joined:
    Feb 5, 2015
    Posts:
    98
    I understand the difference between lists and arrays no need for explanation thanks, but I just wanted suggestions
     
  7. rahulpatil6220375

    rahulpatil6220375

    Joined:
    Dec 10, 2018
    Posts:
    19
    how to instantiate gameobject store in unity and identify duplicated instantiate object ??