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. Dismiss Notice

Question Add gameobject to existing array

Discussion in 'Scripting' started by mixZone, Sep 22, 2023.

  1. mixZone

    mixZone

    Joined:
    Feb 9, 2020
    Posts:
    3
    Hello i have defined array like this
    Code (CSharp):
    1. public GameObject[] object = new GameObject[1];
    how do i add gameobject into this array.

    I have this method
    Code (CSharp):
    1.     void addToArray(Equipment newItem)
    2.     {
    3.         for (int i = 0; i < lasers2.Length; i++)
    4.         {
    5.             lasers2[i] = newItem;
    6.         }
    7.     }
    But then when i add newItem into array it adds newItem into every array object.
     
  2. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    4,449
    Bunny83 likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    First, don't use this identifier. It's just confusing, if it even compiles:

    Second, regarding this:

    GameObjects are reference types so yes, you're just adding the same item to multiple places.



    Value Types vs Reference Types:

    https://forum.unity.com/threads/hel...a-game-object-with-code.1047332/#post-6779456




    Looks like you're making an inventory. Keep this in mind:

    These things (inventory, shop systems, character customization, dialog tree systems, crafting, etc) are fairly tricky hairy beasts, definitely deep in advanced coding territory.

    Inventory code never lives "all by itself." All inventory code is EXTREMELY tightly bound to prefabs and/or assets used to display and present and control the inventory. Problems and solutions must consider both code and assets as well as scene / prefab setup and connectivity.

    Inventories / shop systems / character selectors all contain elements of:

    - a database of items that you may possibly possess / equip
    - a database of the items that you actually possess / equip currently
    - perhaps another database of your "storage" area at home base?
    - persistence of this information to storage between game runs
    - presentation of the inventory to the user (may have to scale and grow, overlay parts, clothing, etc)
    - interaction with items in the inventory or on the character or in the home base storage area
    - interaction with the world to get items in and out
    - dependence on asset definition (images, etc.) for presentation

    Just the design choices of such a system can have a lot of complicating confounding issues, such as:

    - can you have multiple items? Is there a limit?
    - if there is an item limit, what is it? Total count? Weight? Size? Something else?
    - are those items shown individually or do they stack?
    - are coins / gems stacked but other stuff isn't stacked?
    - do items have detailed data shown (durability, rarity, damage, etc.)?
    - can users combine items to make new items? How? Limits? Results? Messages of success/failure?
    - can users substantially modify items with other things like spells, gems, sockets, etc.?
    - does a worn-out item (shovel) become something else (like a stick) when the item wears out fully?
    - etc.

    Your best bet is probably to write down exactly what you want feature-wise. It may be useful to get very familiar with an existing game so you have an actual example of each feature in action.

    Once you have decided a baseline design, fully work through two or three different inventory tutorials on Youtube, perhaps even for the game example you have chosen above.

    Breaking down a large problem such as inventory:

    https://forum.unity.com/threads/weapon-inventory-and-how-to-script-weapons.1046236/#post-6769558

    If you want to see most of the steps involved, make a "micro inventory" in your game, something whereby the player can have (or not have) a single item, and display that item in the UI, and let the user select that item and do things with it (take, drop, use, wear, eat, sell, buy, etc.).

    Everything you learn doing that "micro inventory" of one item will apply when you have any larger more complex inventory, and it will give you a feel for what you are dealing with.

    Breaking down large problems in general:

    https://forum.unity.com/threads/opt...n-an-asteroid-belt-game.1395319/#post-8781697

    The moment you put an inventory system into place is also a fantastic time to consider your data lifetime and persistence. Create a load/save game and put the inventory data store into that load/save data area and begin loading/saving the game state every time you run / stop the game. Doing this early in the development cycle will make things much easier later on.
     
  4. mixZone

    mixZone

    Joined:
    Feb 9, 2020
    Posts:
    3
    I need fixed size of array, thats why i need array
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    You seem to be contradicting yourself. A fixed-size array only stores a fixed amount of things. You seem to be asking how to "add" stuff to a fixed-update. Maybe you're asking how do I add objects to each position until I can no longer do so because the array is full? If so, then you simply have to have a separate field that stores the array position to store the next object.

    Also, what have these two bits of code got to do with each other? The array is named "object" (bad!) but you're adding to an array called "lasers2" so which it is?
    Code (CSharp):
    1.     public GameObject[] object = new GameObject[1];
    2.  
    Code (CSharp):
    1. void addToArray(Equipment newItem)
    2.     {
    3.         for (int i = 0; i < lasers2.Length; i++)
    4.         {
    5.             lasers2[i] = newItem;
    6.         }
    7.     }
     
    APSchmidt likes this.
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Maybe you mean this but if not, please try to explain more clearly what it is you actually want and provide code that clarifies what you want too:

    Code (CSharp):
    1.     private readonly Equipment[] m_Lasers = new Equipment[100];
    2.     private int m_LaserIndex;
    3.  
    4.     void AddToArray(Equipment newItem)
    5.     {
    6.         if (m_LaserIndex == m_Lasers.Length)
    7.             throw new InvalidOperationException("Lasers are full");
    8.  
    9.         m_Lasers[m_LaserIndex++] = newItem;
    10.     }
    This is a lot of extra housekeeping to deal with when you can just a "List<Equipment>" though.
     
    APSchmidt likes this.
  7. mixZone

    mixZone

    Joined:
    Feb 9, 2020
    Posts:
    3
    Thank you