How to use prefab.

I'm trying to make a game where a rolling ball is created dynamically.

I think below code is correct, but every time i run the code, I get an error saying that Ball1 is has not been assigned.

I think I have to use created a prefab object, which was done by Asset->create->prefab and drag and drop an game object on to it, but I have no idea how to use it.

var Ball1 : GameObject;

function Update () {

    print(Buttons.ball1Count);

    if(Buttons.ball1Count > 0){
        var test : Rigidbody = Instantiate(Ball1, Vector3(0,0,15), Quaternion(0,0,0,1));

        test.GetComponent("prefab");

        Buttons.ball1Count --;
    }

}

1 Answer

1

Assign this script to a gameObject (by dragging it from the Project view onto the gameObject). A new tab on the object will appear with the name of your script on it. An empty slot will appear in this tab labelled "Ball 1". Assign your prefab to this slot via drag-and-drop, just as with assigning the script. That should do the trick.

If this works for you, I wouldn't mind an upvote either. :P Glad I could help.

–

Yes, it's because you're trying to Instantiate Ball1 as a rigidbody -- this won't work. If you change var test : Rigidbody = Instantiate to this var test : GameObject = Instantiate the error will disappear.

–

No problem, good luck with your project.

–