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.
Answer by TBruce · Oct 11, 2016 at 07:55 PM
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;
}
}
Answer by deraggi · Oct 11, 2016 at 08:55 PM
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!
Your answer
![](https://koobas.hobune.stream/wayback/20220612094231im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
random an image after a event ends 0 Answers
How to Access Values from a Object,How to access values from a object 0 Answers
I need to activate a function in another script from a script on a different game object 0 Answers
collider.gameObject.GetComponent doesn't have correct values yet it has the correct name 0 Answers
Texture turn black and Gameobject become half or unseen-able 0 Answers