I was looking at some code for a Breakout type game, and I remember seeing something like this:
public Transform ballPrefab;
Can someone help me understand why the ball prefab would be a Transform rather than a GameObject?
Thanks.
I was looking at some code for a Breakout type game, and I remember seeing something like this:
public Transform ballPrefab;
Can someone help me understand why the ball prefab would be a Transform rather than a GameObject?
Thanks.
It can be whatever you like.
If you call Instantiate on a GameObject the return type is a GameObject. If you call it on a Transform the return type is a Transform. If you call it in a custom component you will get a custom component returned.
So normally you make the prefab type match whatever you are trying to do with the prefab. It saves a GetComponent call.
It boil down to the usage of a specific GameObject, that you have in mind.
The game object is in fact a collection of other components; there is no such thing as GameObject.Transform; so there is no real benefit in creating the variable as GameObject, if you are interested in accessing the transform. You use transform directly.
Otherwise you need GetComponent, as mentioned in the previous answer, to access the transform.