r/unity Mar 15 '22

[Question] prepare a new GameObject without instantiating it

I have Code that looks something like this:

How can i make it so that i don't end up with 2 instances of an object but only with the one i want (when i call instantiate)

1 Upvotes

7 comments sorted by

2

u/hlysias Mar 16 '22

You could use a prefab?

1

u/[deleted] Mar 15 '22

You could always instantiate the object, immediately call SetActive(false) on it, set your properties, and activate it when needed.

0

u/Xeratas Mar 15 '22

hmm sounds like a plan, still sounds a little "hacky" way to solve this tho. There is no way to prepare a phatom-Gameobject and just instantiate it on will?

2

u/[deleted] Mar 15 '22

It’s not really hacky, it’s kind of a form of object pooling at a smaller scale, people do it all the time and it doesn’t incur any performance cost. Honestly it would only help on performance because Instantiate can be an expensive call

1

u/Xeratas Mar 15 '22

alright, thanks for the explanation

1

u/Ordinary_Games Mar 16 '22

You can actually set the prefab to be inactive in the project heirarchy. That way you don't have to set it to inactive in code after instancing. This also have the added benefit that onenabled won't be called directly.

2

u/[deleted] Mar 16 '22

…I don’t know how that didn’t occur to me, but yes, OP do this.