Temporary Gameobject Reference in Function without Instantiating

Hey everyone,

this might seem obvious for everyone intermediately experienced with C#, but I’m struggeling a bit with Gameobect variables in functionsproject.

I have a class with multiple GO Attributes, like:

GameObject trees;
GameObect cars;
GameObject houses;

Now, I would like to get the child of ony of those objects in one funtion. This is just an example:

public GameObect getChild(myTypeEnum type) 

 GameObject parent = new GameObject();
        switch (type)
        {
            case (myTypeEnum .house):
                if (houses == null)
                   parent = houses;
                break;
            case (myTypeEnum.cars):
                if (cars == null)
                    parent = cars;
                break;
            case (myTypeEnum.trees):
                if (trees== null)
                    parent = trees;
                break;
            default:
                break;
        }

return parent.transform.GetChild(0)

My problem is now that due to the new Gameobject() statement, a GO is instantiated in the hirarchy which I cannot destroy as I need to return it. I am sure there is a better way to do this…

Anyhelp is much appreciated.

Going by your code you have something like this (note: everything here was placed in one class)

using UnityEngine;
using System.Collections;

public enum ArrayType {house, cars, trees}

public class MyClass : MonoBehaviour
{
    GameObject trees;
    GameObect cars;
    GameObject houses;

    public GameObect getChild(myTypeEnum type)
    {
        GameObject parent = new GameObject();
        switch (type)
        {
            case (myTypeEnum .house):
                if (houses == null)
                    parent = houses;
                break;
            case (myTypeEnum.cars):
                if (cars == null)
                    parent = cars;
                break;
            case (myTypeEnum.trees):
                if (trees== null)
                    parent = trees;
                break;
            default:
            break;
        }
        return parent.transform.GetChild(0)
    }
}

Now to return the first child of one of those objects in the getChild() funtion you can do it like this

using UnityEngine;
using System.Collections;

public enum ArrayType {house, cars, trees}

public class MyClass : MonoBehaviour
{
    GameObject trees;
    GameObect cars;
    GameObject houses;

    public GameObect getChild(myTypeEnum type)
    {
        // if you only want the child you can return null or new GameObject();
        // default to null
        GameObject go = null;
        switch (type)
        {
            case (myTypeEnum .house):
                go = houses;
                break;
            case (myTypeEnum.cars):
                go = cars;
                break;
            case (myTypeEnum.trees):
                go = trees;
                break;
            default:
            break;
        }

        // if go is not null and go has at least one child return it
        // else return go
        if ((go != null) && (go.transform.childCount > 0))
        {
            return houses.transform.GetChild(0).gameObect;
        }
        else return go;
    }
}

Actually, this

GameObject go = null;

was what I needed. Did not think of that myself and were not able to find it.

Thanks you so much!