Hi all.
I want to instantiate a bulb when a collision enter. but OnTriggerEnter() works
correctly but when I add a piece of code that Instantiate a Object the compiler show some errors:
(Filename: Assets/MyScripts/Animals.cs Line: 21)
Assets/MyScripts/Animals.cs(21,25): error CS1502: The best overloaded method match
for `UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3,
UnityEngine.Quaternion)’ has some invalid arguments
(Filename: Assets/MyScripts/Animals.cs Line: 21)
Assets/MyScripts/Animals.cs(21,25): error CS1503: Argument #1' cannot convert object’ expression to type `UnityEngine.Object’
(Filename: Assets/MyScripts/Animals.cs Line: 21)
the code is: bullet sp = Instantiate( bullet, transform.position, transform.rotation); sp.transform.localScale.Scale(new Vector3(radius , radius , radius));
I have read the Script reference and Documentation but i confused why this code has type conversion error.
please Help me.
thanks for your help.
You have actually tried to create an instance using the type definition (“bullet”).
You can not do that.
You could however do something like the following:
// In the edito, create a prefab of type bullet and drag it to the GameObject on which you have attached this script
public bullet referenceToBulletPrefab;
void OnTriggerEnter(Collider other)
{
// Put here the rest of the code
// Create a new instance of a bullet using the prefab: referenceToBulletPrefab
bullet sp = Instantiate( referenceToBulletPrefab, transform.position, transform.rotation);
// Put here the rest of the code
}
Thanx for your answer but the problem when occured the Instantiate() return UnityEngine.Object and isn’t a prefab such as bullet!! I’m hanging on it!!!
We don’t know what type the instantiated bullet is, but whatever it is, newbullet is declared as one of those. In C#, you just have to know Instantiate returns a gameObject, and how to convert that to a Transform (this is in the Instantiate ex in the Unity manual, if you switch it to C#):
Transform newbullet = Instantiate(bulletPrefab .... ) as Transform;
newbullet.rigidbody.... // can now use it like a normal Transform