Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to add stuff to GameObject[].

Discussion in 'Scripting' started by StarGamess, Jan 26, 2015.

Thread Status:
Not open for further replies.
  1. StarGamess

    StarGamess

    Joined:
    Jul 21, 2014
    Posts:
    179
    Oke, So how do i add a gameobject to a list of gameobjects.
    Code (CSharp):
    1. public GameObject [] gameobjectslist;
    Like this how can i add gameObjects to this. Or if the list excists of scripts like this:
    Code (CSharp):
    1. public someScript[] nameOfvar;
    know this is very basic question but i really dont know and i would like to know.
     
    Last edited: Jan 26, 2015
  2. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    264
    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<GameObject> might be better for your task.
     
    Ebil and angrypenguin like this.
  3. StarGamess

    StarGamess

    Joined:
    Jul 21, 2014
    Posts:
    179
    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.
     
  4. StarGamess

    StarGamess

    Joined:
    Jul 21, 2014
    Posts:
    179
    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
     
    Last edited: Jan 26, 2015
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    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.
     
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    ^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
     
  7. StarGamess

    StarGamess

    Joined:
    Jul 21, 2014
    Posts:
    179
    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.
     
  8. StarGamess

    StarGamess

    Joined:
    Jul 21, 2014
    Posts:
    179
    Oke so i cant get it to work this my script that creates the list:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class ItemManager : MonoBehaviour
    6. {
    7.  
    8.     public List <ItemCollector> Items;
    9.     void Start(){
    10.      
    11.     }
    12.     void Awake()
    13.     {
    14.  
    15.         //Debug.Log("Setting All Items");
    16.     }
    17. }
    18.  
    and this is the itemcollector script
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public enum ItemType
    5. {
    6.     Weapon    = 0,
    7.     Edible    = 1,
    8.     Misc = 2,
    9. }
    10. public enum WeaponType
    11. {
    12.     Melee    = 0,
    13.     Ranged    = 1,
    14. }
    15.  
    16. [System.Serializable]
    17. public class ItemCollector
    18. {
    19.     public Texture2D Icon;
    20.     public string Name = "Item Name";
    21.     public string Description = "Description";
    22.     public int Price;
    23.     public ItemType ItemType;
    24.     public GameObject ItemPrefab;
    25.     public GameObject ItemPrefabDrop;
    26. }
    27.  
    28. public enum ItemRenderMode{
    29.     Player,
    30.     Sell,
    31.     Buy  
    32. }
    now how can i add one to this list it just wont work idk why.
     
  9. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Code (CSharp):
    1. void Start(){
    2.      Items = new List<ItemCollector>();
    3.      Items.Add(new ItemCollector() );
    4.     }
    Or more likely
    Code (CSharp):
    1. void Start(){
    2.      Items = new List<ItemCollector>();
    3.      ItemCollector newItem = new ItemCollector();
    4.      newItem.Name = ...
    5.      newItem.Price = ...
    6.      Items.Add(newItem);
    7.     }
     
    StarGamess likes this.
  10. DChristy87

    DChristy87

    Joined:
    Jan 23, 2014
    Posts:
    19
    public List <ItemCollector> Items = new List<ItemCollector> ();
    Items.Add(whateverItem);
     
  11. StarGamess

    StarGamess

    Joined:
    Jul 21, 2014
    Posts:
    179
    that wont work... that is what i thought but it gives me an error.
     
  12. StarGamess

    StarGamess

    Joined:
    Jul 21, 2014
    Posts:
    179
    that looks like it can work i will try it tonight thanks man!
     
  13. DChristy87

    DChristy87

    Joined:
    Jan 23, 2014
    Posts:
    19
    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?
     
  14. StarGamess

    StarGamess

    Joined:
    Jul 21, 2014
    Posts:
    179
    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?
     
  15. StarGamess

    StarGamess

    Joined:
    Jul 21, 2014
    Posts:
    179
    thanks man it worked.
     
  16. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    264
    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!
     
    Last edited: Jan 28, 2015
  17. StarGamess

    StarGamess

    Joined:
    Jul 21, 2014
    Posts:
    179
    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.
     
  18. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    264
    Wow! Interesting reaction! Okay, I know when to stop discussing.
     
    StarGamess likes this.
  19. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    :(
     
  20. StarGamess

    StarGamess

    Joined:
    Jul 21, 2014
    Posts:
    179
    haha srry i forgot about you thank you aswell you helped a lot, a lot more actually haha xD srry man.
     
Thread Status:
Not open for further replies.