- Home /
Problem with Instantiate() Function in C#
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 convertobject' 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.
Answer by GuyTidhar · Sep 13, 2011 at 07:18 AM
You need to give the Instantiate function a Unity object (usually a prefab) from which unity will create the new instance. In the following case:
bullet sp = Instantiate( bullet, transform.position, transform.rotation);
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
}
I try it but it doesn't work at all. I have create bullet prefab define a public Transform variable and attach bullet prefab to Transform variable but the errors is the same.
Is the prefab root a GameObject on which your script "bullet" is attached?
I change the language from C# to JavaScript. Your $$anonymous$$ethods works Correctly. thank you. :)
Cool man. Glad to had helped.
Note that it is accustomed to mark an answer as correct (if it is) + give a thumbs up.
Answer by MohsenEbrahimi · Sep 13, 2011 at 12:06 PM
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' ins$$anonymous$$d of an-answer-to-your-question for such annotations.
Answer by Owen-Reynolds · Sep 13, 2011 at 02:50 PM
You can still use C#, if you want. Most people in javascript use untyped variables, which works great when you don't know the type. You get to say:
public bulletPrefab : Transform;
newbullet = Instantiate( bulletPrefab ..... );
newbullet.regidbody.velocity = ....
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
Your answer
Follow this Question
Related Questions
Targetting script errors 1 Answer
Distribute terrain in zones 3 Answers
error with GUI in c sharp 2 Answers
CS1502 and CS1503 Error 0 Answers
Multiple Cars not working 1 Answer