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.
As the error states, the problem lies with the 1st argument, which is the variable "bullet". That variable is not of type "UnityEngine.Object" which means that you've likely declared it to be something else. The usual approach here is to declare a public variable of type Transform in the script, and then drag a prefab onto that variable in the editor. Transform is of type UnityEngine.Object. Use that.
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
}
Is the prefab root a GameObject on which your script "bullet" is attached?
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!!!
please use 'add a comment' instead of an-answer-to-your-question for such annotations.
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
As the error states, the problem lies with the 1st argument, which is the variable "bullet". That variable is not of type "UnityEngine.Object" which means that you've likely declared it to be something else. The usual approach here is to declare a public variable of type Transform in the script, and then drag a prefab onto that variable in the editor. Transform is of type UnityEngine.Object. Use that.
– CHPedersen