Search Unity

Sprite CardList

Discussion in 'Scripting' started by klausbous, Sep 7, 2017.

  1. klausbous

    klausbous

    Joined:
    Sep 7, 2017
    Posts:
    16
    Hello,
    I'm quite new to unity coding side and i'm trying to learn how i would select random 5 images(Cards) from a array and render them on the screen. Can someone give me some help to get started? This is what i have so far. I don't quite understand how i can make random.range use the array.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DrawCards : MonoBehaviour {
    6.  
    7.     public Sprite[] CardList;
    8.  
    9.     // Use this for initialization
    10.  
    11.      
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update () {
    16.      
    17.     }
    18.  
    19.     void OnGUI(){
    20.  
    21. }
    22. }
    23.  
     
    Last edited: Sep 7, 2017
  2. klausbous

    klausbous

    Joined:
    Sep 7, 2017
    Posts:
    16
    I found out how :)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DrawCards : MonoBehaviour {
    6.  
    7.     public Sprite[] CardList;
    8.     public SpriteRenderer CardRenderer;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         CardRenderer.sprite = CardList [Random.Range (0, CardList.Length)];
    13.    
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update () {
    18.        
    19.     }
    20.  
    21.     void OnGUI(){  
    22.  
    23. }
    24. }
    25.