How to add stuff to GameObject[].

Oke, So how do i add a gameobject to a list of gameobjects.

public GameObject [] gameobjectslist;

Like this how can i add gameObjects to this. Or if the list excists of scripts like this:

public someScript[] nameOfvar;

know this is very basic question but i really dont know and i would like to know.

Well, you might not want to hear that but you definitely should learn programming in general first. Buy a book about C# and learn the basic principles first!

Note: You shouldn’t use an array if you don’t really need to. A List might be better for your task.

2 Likes

Could you pls just anwser my question i need this and not a list i know alot im pretty good at programming i just dont know this little bit how can i add things to arrays. I know how to edit them but not how to add stuff to them.

Nvm i found the anwser once i knew it was called an array in english… I thought you could use a ArrayName.Add but apperently that only works for lists. Anyway you can just do +[obj] and it’ll add one, but you cant change the size of the array so i will have to switch over to lists which will be a pain in the ass since a ton of my code is based on that variable being an array… haha

Gameobjects cannot hold gameobjects. Gameobjects can have components plugged into them (such as scripts).

You can parent one game object’s transform to another game object’s transform. This is appropriate if there is a scene graph dependency hierarchy between the components. In the editor this is accomplished by parenting. In code this is accomplished by setting the transform.parent to the other object’s transform.

You can write a script ( a component) that contains some type of either array, list or other sequence container that contains references to game objects. In the editor this is done by exposing the list as public and dragging the desired “things” into it, which could be GameObjects or any other Component. In code you would simply use whatever was appropriate to add a reference to the item to your list/array.

^ignore this - not relevant

Yeah, you cannot ‘add’ to a GameObject[ ] array, only change the items already in the array, change to List, or:
You could write a method that outwardly looks like an Add method, which creates a new, bigger GameObject[ ] array, copies the old array into the new array (system.array.copyTo), and puts the gameobject in the last position

Yupp switched to list i thought i had to change a ton of code but apperantly i only need to change .Length into .Count. Everything else is exactly the same it even kept all the script and variables that were in the array. I never used list but they are so much easier then arrays i will definitely use them more often now.

Oke so i cant get it to work this my script that creates the list:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ItemManager : MonoBehaviour
{
  
    public List <ItemCollector> Items;
    void Start(){
      
    }
    void Awake()
    {

        //Debug.Log("Setting All Items");
    }
}

and this is the itemcollector script

using UnityEngine;
using System.Collections;

public enum ItemType
{
    Weapon    = 0,
    Edible    = 1,
    Misc = 2,
}
public enum WeaponType
{
    Melee    = 0,
    Ranged    = 1,
}

[System.Serializable]
public class ItemCollector
{
    public Texture2D Icon;
    public string Name = "Item Name";
    public string Description = "Description";
    public int Price;
    public ItemType ItemType;
    public GameObject ItemPrefab;
    public GameObject ItemPrefabDrop;
}

public enum ItemRenderMode{
    Player,
    Sell,
    Buy   
}

now how can i add one to this list it just wont work idk why.

void Start(){
     Items = new List<ItemCollector>();
     Items.Add(new ItemCollector() );
    }

Or more likely

void Start(){
     Items = new List<ItemCollector>();
     ItemCollector newItem = new ItemCollector();
     newItem.Name = ...
     newItem.Price = ...
     Items.Add(newItem);
    }
1 Like

public List Items = new List ();
Items.Add(whateverItem);

that wont work… that is what i thought but it gives me an error.

that looks like it can work i will try it tonight thanks man!

Just to be sure, Are you declaring your list at the top inside of your class and adding your item inside of your Start()? Also, what error is it giving you?

no i like to add stuff to the list and when i add one i want to change the variables. The class is the list you see?

thanks man it worked.

I don’t want to make you angry but in my opinion you should take a critical look at yourself:

Now, you are using a List and you agreed in that it is easier to use.

You don’t know a lot about programming if you don’t know how to use an array or a list!

i didnt need list but an array then someone told me an array was impossible so now i have to use a list. And it is easy to use never said lists werent easy. Hahaha looked at myself very very criticly and i came to the conclusion that you are a ******. But dont worry these forums are fluded with people like you infact every forum is. I got used to it. Oh let me re phrase that i know enough about programming to have fun with it when i want to make the games i want and if that fact that i never used a list because i didnt know they existed makes me not know alot about it in your opinion than good for you i hope you are now happy… Because i certainly am ;D

But thanks again DChristy87 for the helpful anwser.

Wow! Interesting reaction! Okay, I know when to stop discussing.

1 Like

:frowning:

haha srry i forgot about you thank you aswell you helped a lot, a lot more actually haha xD srry man.