How do I spawn prefabs in a 2d scene?

I have been googling for a while now and found it has something to do with the function instantiate.

I made a prefab of the object I want to spawn and created the following code.

 #pragma strict
    var fruit : Rigidbody ;
    var moveLeft 		: KeyCode ;
    var moveRight 	: KeyCode ;
    var spaceBar : KeyCode ;
    var speed : float = 10 ;
    function Update () 
    	if 		(Input.GetKey(moveLeft))
    	 	rigidbody2D.velocity.x = speed;
    	else if (Input.GetKey(moveRight))
    	 	rigidbody2D.velocity.x = speed*-1;
    		rigidbody2D.velocity.x = 0;
    	if (Input.GetKeyDown(spaceBar))
    	var clone : Rigidbody;
    	 clone = Instantiate(fruit, transform.position, transform.rotation);

The prefab I want to spawn is called fruit, unity says: UnassignedReferenceException: The variable fruit of Playercontrols has not been assigned.

Any help would be appreciated. (also, I’m new to unity so it’s probably something obvious that I’m doing wrong.)

With unity in editor mode(not play mode) have you assigned(dragged and dropped) the prefab from your assets folder to the Fruit in the inspector.

Also to make things easier, you might want to go ahead and use a GameObject type instead of a RigidBody type, you can gain access to the RigidBody easily(fruit.rigidbody) if you needed to.

I’m not 100% sure if these vids will help you, but check out the 2d infinite runner tutorial in the learn section -

also the 2d catch game -

I learnt a thing or two from them…goodluck