2
votes

I am trying to generate multiple sprites in my scene using just a single sprite. I found it difficult, when I tried using array of sprite and the sprite renderer, because in my scene I only see a single sprite. I also want to display the sprites in different areas in my Unity Scene.

The Main Code:

using UnityEngine;
using System.Collections;

public class SampleCode : MonoBehaviour {

    float posX = -4.5F;
    float posY = -0.65F;

    public IEnumerator Start(){
        for(int i = 0; i < 5; i++){
            posX = posX + 0.65F;

            if(posX > 3.5F){
                for(float x = 0.50F; x < 2.5F; x = x + 0.50F){
                    posY = -1.5F + x;
                    posX = -4.15F + x;;
                }
            }

        string path = "file:///C:/Users/Pankaj Sharma/Documents/untitled.bmp";
            SpriteRenderer rend = this.GetComponent<SpriteRenderer>();
            Sprite sprite = new Sprite();
            WWW www = new WWW(path);
            yield return www;
            sprite = Sprite.Create(www.texture, new Rect(0, 0, 50, 50),new Vector2(posX, posY),100.0f);
            rend.sprite = sprite;
        }
    }   
}

1

1 Answers

1
votes

You only have one SpriteRenderer that's why you only see one sprite.

If you have multiple objects in your scene with a SpriteRenderer component you can set your sprite to the GetComponent<SpriteRenderer>().sprite for the objects you want in your scene.

Does this make sense to you?