- Home /
Help with Instantiating
Alright people, when all else fails go to the Answers. This should be easy for anybody more experienced than me. I sort of understand what the problem is but I can't think of what I have to do to fix it. projectilePrefab is a GameObject and changing it to an object doesn't resolve the issue. The error is as follows:
Assets/First-Third Person Shooter/Resources/Scripts/Weapon/Gun.cs(273,28): error CS0266: Cannot implicitly convert type UnityEngine.Object' to
UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)
GameObject projectile = GameObject.Instantiate(projectilePrefab, startPosition, Quaternion.identity);
Basically, what you are doing is taking a GameObject (your prefab) and telling Unity to set it to an Object function. So, like the answer below, as "as GameObject" to the end to tell unity to take a GameObject and apply a GameObject function to it.
Answer by Polymo · Aug 29, 2013 at 02:00 PM
GameObject projectile = GameObject.Instantiate(projectilePrefab, startPosition, Quaternion.identity) as GameObject;
The reason this is the answer is that GameObject.Instantiate returns an Object. Then you have to cast it back to whatever type you are instantiating, in this case, a GameObject
Or you can just use
GameObject projectile = (GameObject)Instantiate(projectilePrefab, startPosition, Quaternion.identity);
Just another way to get the same result ;)