Why do we need as GameObject?

Why do we need the as GameObject?

GameObject laser = Instantiate(laserPrefab, transform.position, Quaternion.identity) as GameObject;

It works for me without it - what’s the meaning of this reference?

Thanks

1 Like

Good question! Following for the answer.

1 Like

Hi Niv, hi Jackie,

In past versions of Unity, casting was required. This was changed in one of the Unity 2017 versions (I think). Game developers who have been using Unity for years still cast with “as GameObject” out of habit. It’s not wrong to do that, just not required anymore.

Has your question been answered?


See also:

5 Likes

Thanks :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms