0

I have a function that generates a game object on screen every 100 frames:

 var trash : Transform ;
 function Update()
 {
      if(count == 1)
      {
         Instantiate(trash,new Vector3(UnityEngine.Random.Range(-3f,3f),UnityEngine.Random.Range(-3f,3f),UnityEngine.Random.Range(-3f,3f)), Quaternion.identity);
      }

     else if(count == 100)
     {
         count = 0;
     }
 }

Now, I want each of those generated objects to pick a random sprite from my assets. I thought about making a sprite array and picking from that, but I'm not sure how to go about doing so or how to assign the sprite to the object.

Anu help would be much appreciated!

user3071284
  • 6,955
  • 6
  • 43
  • 57
iVikD
  • 296
  • 7
  • 21
  • See [this answer](http://stackoverflow.com/a/5915122/1024766)! – soulprovidr Nov 10 '14 at 21:10
  • @sholanozie Thanks, that takes care of selecting from the array, now I just need to be able to assign it to the object. I'm not sure if that has to be done from within the Instantiate method or otherwise. – iVikD Nov 10 '14 at 21:14
  • 1
    I'm not experienced with Unity, but [this page](http://docs.unity3d.com/Manual/InstantiatingPrefabs.html) seems like it might be what you're looking for. – soulprovidr Nov 10 '14 at 21:20
  • It helped me find a workaround, by making a prefab for each kind of sprite. Technically, it works, I've tested it already, but it's not very efficient, as I now have a random number generator to pick from 20 prefabs. Still, thanks a bunch! – iVikD Nov 10 '14 at 22:14
  • 1
    Look up SpriteRenderer.sprite – Savlon Nov 11 '14 at 11:33

2 Answers2

1

I know it's late, but I just did it for myself, so I'm sharing.


Basically - put your assets in a folder and choose randomly from them to assign to the object you're duplicating.

  1. Create a Resources folder under Assets.
  2. Create another sub-folder for your relevant sprite assets, under Resources, lets call it CookiesImages.
  3. Create your prefab object, and locate it under Assets/Prefabs/Resources (this is the object that you want to duplicate multiple times with different assets).
  4. Create a script for this prefab object.
  5. Create a static Object array (will hold the sprites).
  6. In your Awake() method, init this Object array by loading all the resources from your assets folder:

    if (objectsArray == null)
    {
        objectsArray= Resources.LoadAll("CookiesImages", typeof(Sprite));
    }
    
  7. In the Start() method, get a random index 0 --> objectsArray.Length and call Instantiate to create a sprite to assign to your prefab object.

    this.GetComponent<SpriteRenderer>().sprite = Instantiate(objectsArray[index]) as Sprite;
    

At the beginning, I tried assigning them to an array variable from the unity UI, but I realized that in the way I described it's much more flexible.

arieljannai
  • 2,124
  • 3
  • 19
  • 39
0

Well, you can generate an array of sprites of the length of your choosing with: Sprite[] _sprites = new Sprite[quantity desired]

Then using the resources folder and the Resources.Load method (found here: http://docs.unity3d.com/ScriptReference/Resources.Load.html), you could load all your sprites into the array one after the other. Then using your random number generator, you can access sprites at random from it.

Still probably not the best way of doing it, but I would imagine it is considerably better and easier to maintain than the prefab method, but different situations call for different approaches.

Benjamin James Drury
  • 2,353
  • 1
  • 14
  • 27
  • Also I can't help but notice there isn't actually anything in your update method to increment count, is that intended for this example or...? – Benjamin James Drury Nov 10 '14 at 22:46
  • That does sound more efficient, I'll give it a shot. No, there's supposed to be a count++, I guess I missed it when I copied it over... – iVikD Nov 10 '14 at 23:10
  • If you feel like taking it a step further, you could format the string you used in Resources.Load so that it contains the number from your random generator at the end. Then when you put the sprites into your resources folder, you can just add a number to the end of each one. Honestly don't know if that will be quicker, but if memory usage is an issue it could do the trick. – Benjamin James Drury Nov 10 '14 at 23:15