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

Adding unknow GameObjects to a List or Array.

Discussion in 'Scripting' started by Ben Norman, Oct 12, 2015.

  1. Ben Norman

    Ben Norman

    Joined:
    Sep 8, 2015
    Posts:
    9
    Hi people of Unity!

    My project where i'm working for is more model based as gaming related.
    I'm trying to extract information from a model made in Unity to a .txt file.
    At the moment, it works pretty well. I just need some little changes but i struggle with adding ALL the GameObject to an array (or list). I only can add one tag or just one object to the array (or list).

    (Wall and Floor are just examples. The names of the objects are unknown)
    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using UnityEditor;
    4. using UnityEngine.UI;   //For Canvas, InputField, ..
    5. using System.Collections.Generic;   //For list
    Code (CSharp):
    1. public GameObject[] objecten;
    Code (CSharp):
    1.         objecten = GameObject.FindGameObjectsWithTag("Wall");   //Need change tag to everything exept "Untagged"
    2.         objecten = GameObject.FindGameObjectsWithTag("Floor");  // += doesn't work
    3.  
    If i do this, i only get the GameObjects with the "Floor" tag in my list called "objecten".
    When I try it with a list in stead of an array, both Floor and Wall tagged GameObjects will be added to the list, but the problem is that i need to know the names of the GameObjects. (or how many taggs there will be used) This isn't known, becaus I can't know how many GameObjects it will take to make the model and how the user will name the Objects.

    Code (CSharp):
    1.     List<GameObject> objecten = new List<GameObject>();     //empty list to begin with.
    2.  
    Code (CSharp):
    1.         objecten.Add(GameObject.Find("Wall"));
    2.         objecten.Add(GameObject.Find("Floor"));
    3.  
    I'm already searching for 3 days in the tutorials, forums, answers and youtube to find a solution but i don't find anything relevant.

    I thought about different ideas, but I can't translate them into code.
    1: Let the user use taggs. Place all gameobjects except the untagged into a list/array
    2: Extract all the names of the gameobjects (names go to list/array and i = length of array/count of list). Foreach name in list/array: objecten = GameObject.FindGameObjectsWithTag(" the name of gameobject ").
    3: Mayby some wierd shizzle with Renderer, becaus every Object has a renderer component.

    And yes I need to do this with Unity. I know there are better programs to make models to extract information from, but i'm only able to use Unity for this project.
    Thanks for your time. I hope you can help me.
     
  2. Carvuh

    Carvuh

    Joined:
    Mar 25, 2013
    Posts:
    25
    Ben Norman likes this.
  3. Ben Norman

    Ben Norman

    Joined:
    Sep 8, 2015
    Posts:
    9
    Is there a way to make it easier for the user and extract the names from somewhere? I was thinking to get them from the hierarchy, but you can't get to those information

    Already thanks for your time and effort :)
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    Code (csharp):
    1. List<GameObject> objecten =new List<GameObject>();
    2.  
    3. objecten.AddRange(GameObject.FindGameObjectsWithTag("Wall"));
    4. objecten.AddRange(GameObject.FindGameObjectsWithTag("Floor"));
    5. ...etc
    AddRange adds everything in a collection to the list.

    There's also the brute-force approach:

    Code (csharp):
    1. GameObject[] allObjects = FindObjectsOfType<GameObject>();
     
    Ben Norman likes this.
  5. Ben Norman

    Ben Norman

    Joined:
    Sep 8, 2015
    Posts:
    9
    Thanks guys!
    You expanded my programming mind :)