- Home /
Instantiating Prefab from Javascript - BCE0005: Unknown identifier: 'Prefab'
Hello World!
When I try to instantiate a prefab from a script, I receive error message BCE0005: Unknown identifier: 'Sphere1'.
I have made the prefab so I don't understand why I'm getting the error message.
Here's how I made the prefab:
Right clicked inside the "Project View" window->selected create->prefab.
Clicked on GameObject->Create Other->Sphere.
Dragged sphere in "Hierarchy" window onto prefab in "Project View" window.
Here's how I'm instantiating the prefab from the script:
function OnCollisionEnter(collision : Collision) {
var sphere_bullet : GameObject ; sphere_bullet = Instantiate(Sphere1,transform.position, transform.rotation) ; }
What I've done so far to try and solve the problem:
Checked "Overview: Instantiate" in Unity manual to see if I missed something:
http://unity3d.com/support/documentation/ScriptReference/index.Instantiate.html
Checked Overview: Script compilation (Advanced) in manual after reading some questions similar to mine:
http://unity3d.com/support/documentation/ScriptReference/index.Script_compilation_28Advanced29.html
Found out that Unity compiles scripts in a certain order leaving in "editor" scripts for last.
Tried changing position of prefab and script in case the script was being compiled before Unity knew anything about the prefab.
Created a new project with a new script and prefab made the same way without any other folders or assets.
Searched Unity Answers for people with the same problem but I couldn't find a solution that worked for me.
Other Information:
The script code isn't contained in any other functions.
The only objects in the "Project View" window are the prefab and the script.
I named the prefab "Sphere1".
Any help would be great.
You named the prefab sphere1, but did you get the Sphere1 a var name? Like, above the instantiate function, did you do var Sphere1 = ___?
Answer by flaviusxvii · Apr 12, 2011 at 10:11 PM
You need something like:
public var Sphere1:GameObject;
in your script. Then you need to link your prefab to that variable in the script (drag and drop).
Thank you for the help. It worked after I added this into the code. I was ecstatic! I can make stuff instantiate now!
Answer by DaveA · Apr 12, 2011 at 10:13 PM
You mentioned editor scripts, is yours an editor script? If so, did you look here: http://unity3d.com/support/documentation/ScriptReference/EditorUtility.InstantiatePrefab.html
Thanks for the help. You didn't answer my question but I tried running the script you gave me. It works really nicely for quickly generating prefabs with a 1-2 click.