- Home /
Need help with this error
I am getting an error with this code saying variable prefabBullet isn't defined. Explain.
"#pragma strict
function Start () {
} var prefabBullet:Transform;
var shootForce:float;
function Update () {
if(Input.GetButton("Fire1"))
{
var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity);
instanceBullet.rigidbody.AddForce(transform.forward * shootForce);
}"
@BlackWingsCorp sorry.. you're wrong.
@maroonfire Can you post the exact error message the compiler gives you. And all of your code? Put it on Pastebin.com maybe?
@flavius it actually does have to be rigid body and here is the error word for word.
You probably need to assign the prefabBullet variable of the Shoot script in the inspector. UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/BuildAgent/work/812c4f5049264fad/Runtime/ExportGenerated/Editor/UnityEngineObject.cs:44) UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/BuildAgent/work/812c4f5049264fad/Runtime/ExportGenerated/Editor/UnityEngineObject.cs:53) Shoot.Update () (at Assets/Shoot.js:15)
here is the code
"#pragma strict
function Start () {
} var prefabBullet:Transform;
var shootForce:float;
function Update () {
if(Input.GetButton("Fire1")) { var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity); instanceBullet.rigidbody.AddForce(transform.forward * shootForce); } }
Answer by 1337GameDev · Feb 19, 2013 at 03:57 PM
Use resource.load to load the prefab at runtime or assign it via the inspector. Prefabs reference outside resource gameObjects. If you call instantiate on an unitialized prefab reference, it will give this error.
Answer by flaviusxvii · Feb 19, 2013 at 07:32 PM
In your initial explanation you said:
code saying variable prefabBullet isn't defined
And then when you pasted the actual message it said:
You probably need to assign the prefabBullet variable of the Shoot script in the inspector.
Something not being "defined" is a very different error than something not having a value set. It's best not to rephrase error messages when conveying them to others.
Assign a value to bulletPrefab in the inspector, and make sure the prefab you assigned has a rigidBody attached!
Do not yell at other users. Especially new users. This forum is meant to encourage questions not chastize people for making mistakes in being clear. Just advise him to not change error messages or to just post exact console log message.
This is meant to be a learning community. A positive one that encourages question answering and asking. Please abide by what this community is meant to be.
$$anonymous$$y only interest is being helpful. If I didn't care I wouldn't bother commenting/answering. Editorializing warnings/errors from a compiler is a cardinal sin in clarity. Hence the BOLD UPPER CASE. Downvote me if you like. But I don't like coddling the new users and handing them code. They need to learn how to ask clear and specific questions.
Yes you can still not coddle a new user without yelling at them. BOLDING A STATE$$anonymous$$ENT IS YELLING ON THE INTERNET. Especially if its bold. You can $$anonymous$$ch somebody something without being negative. And handing a user a small snippet of code to correctly instantiate is easy and not coddling. They didn't know how to use a function or know that you had to use it a certain way. If he asked for shooting a bullet and making it explode on impact, I can understand you want him to learn by coding himself. But I think acting this way on principle for something so petty is immature.
Just please treat all users positively in the future please. When I started out, I hated people like you that would make it a negative experience to learn.
I edited my post top be less of an asshole.
Also, I think you mean "it worked, asshole". Critical comma there. ;)
Your answer