Creating a GameObject using imported assets

I have an imported asset (a .png file: “castle.png”). It is imported as a multiple sprite. I need to create a 2D Object in a script:

GameObject test = new GameObject ("test");

SpriteRenderer renderer=prueba.AddComponent<SpriteRenderer> ();

//Now I have a Sprite object, but...

renderer=????

My problem is that I don’t know how to tell Unity that the renderer must be, for example, the sprite castle_0

You need to set the sprite with renderer.sprite, given you have a Sprite.

renderer.sprite = castle_0;