The question is answered, right answer was accepted
Pool Instantiate
Ok, so my pooling script has this psyeudo instantiate method. How can have this method behave more like the normal instantiate. So that following 2 things are possible:
// it returns the object
GameObject temp = pool1.Instantiate( pos1, Quaternion.identy) as Gameobject;
//or
GameObject temp = (GameObject ) pool1.Instantiate( pos1, Quaternion.identy);
// it doesnt return anything
poolScript.Instantiate( pos2, Quaternion.identy);
code:
// pseudo instatiate that object
public void Instantiate(Vector3 position, Quaternion rotation)
{
// GetPooledObject gives me the next avaible obj in the pool and returns it
GameObject temp = GetPooledObject ();
temp.transform.position = position;
temp.transform.rotation = rotation;
temp.SetActive (true);
}
Answer by Raresh · May 05, 2016 at 06:47 PM
public GameObject Instantiate(Vector3 position, Quaternion rotation)
{
// GetPooledObject gives me the next avaible obj in the pool and returns it
GameObject temp = GetPooledObject ();
temp.transform.position = position;
temp.transform.rotation = rotation;
temp.SetActive (true);
return temp;
}
The reason is that you were returning void. You need to return a GameObject
Thanks, surpringly that works. Didnt knew you didnt have to assign the return value of a method.
still out of curiosity, how is the default instantiate coded that it needs the extrabit of (gameobject) to have something assigned to it?
That is called a cast. Instantiate actually requires an Object and returns an Object. You need to cast it into a GameObject in order to be usable. GameObject derives from Object, so you can cast it without any issues.
You might want to read a book on the basics of program$$anonymous$$g, because these questions point out that you don't understand the basics of program$$anonymous$$g and OOP. It will save you a lot of time after you read about it.
Follow this Question
Related Questions
Procedurally generating cubes 1 Answer
Issues with my Object Pooler 1 Answer
Accessing the script of an instantiated object 1 Answer
Instantiate a Prefab along the surface of a sphere. C# 0 Answers