Hello all ,
so I am trying to make a game and I need this piece of code:
GameObject obj = (GameObject)Instantiate(pooledObject);
obj.SetActive(false);
pooledObjects.Add(obj);
this code is repeated more than one time , so I thought of putting it into a function (AddItem)
However, in some cases , i need to use the obj variable and some others not
so I need to sometimes return obj and function is as Gameobject and some other times I need is as void as it doesn’t return anything.
I recall learning something called templates in Advance Programming course in C++
After some search I found that It is referenced as Generic functions in C# / Unity
so far I got this :
public T fillList<T>(T param)
{
GameObject obj = (GameObject)Instantiate(pooledObject);
obj.SetActive(false);
pooledObjects.Add(obj);
// return obj;
}
now my problem is that return obj is giving me an error , i am very unfamiliar with the syntax and way of writing generic functions , how can I remove the param and return the obj when it is called as Gameobject?
Can anyone please help?
Thanks for all in advance!