2
votes

Hi I am using unity and javascript to write my code for a card game. After dealing the cards I want to change the players card to random cards in the deck. I have all the sprites created in my assets folder. I was wondering if there was a way to change the sprite by name or something to achieve this. All the cards are called like cardDiamonds8 or cardClubs4. I have already set up to get the name:

#pragma strict
public var testCard : GameObject;
private var suit;
private var value;
private var number : int;



function Start () {
    number = Random.Range(1,5);
    if (number == 1) {
        suit = "Clubs";
    } else if (number == 2) {
        suit = "Diamonds";
    } else if (number == 3) {
        suit = "Hearts";
    } else if (number == 4) {
        suit = "Spades";
    }
    number = Random.Range(1,14);
    if (number == 1) {
        value = "A";
    } else if (number == 11) {
        value = "J";
    } else if (number == 12) {
        value = "Q";
    } else if (number == 13) {
        value = "K";
    } else {
        value = number;
    }
    testCard.name = "card" + suit + value;
    print(testCard.name);
}

This just prints out the name, but the logic is there. Any help to change the sprite would be appreciated.

2
Checkout this answer. It may help you - Hamza Hasan

2 Answers

0
votes

I found a way, but in order to do it you must put all your sprites that you want to use in a folder called 'Resources'. Then just use

gameObject.GetComponent.<SpriteRenderer> ().sprite = Resources.Load("name", typeof(Sprite)) as Sprite;;
0
votes

One method that i would suggest is to create an object that can hold all your sprites. Either utilize the inspector to reference the images, or use the Start() function to load them into the object via code. The reason for this would be to reduce the need to load assets at run time.

Once you have the sprites loaded, you can change them around at run time to you hearts content.

Look into this for loading via code: Resources.Load <Sprite>("YourImageName");

As for changing the sprite via code: you're going to need to get the component on the game objects that are displaying the sprite. If you're using a gui objects then you'll need to get the associated image component and change the reference of the image component.


Update for more clarification, and to improve upon my potentially vague first response.

A suggestion would be the following, which should load your sprites prior to the first execution of the Update() loop.

public var Sprites: Sprite[];

function Start () {
     var imports : Object[] = Resources.LoadAll("path/" , Sprite);
     Sprites = new Sprite[imports.Length];
     for(var i = 0 ; i < Sprites.Length ; i++){
         Sprites[i] = imports[i];
     }
}

Once loaded, you will be able to reference your sprites by utilizing Sprites[idx].

This will let you reference your sprites on the fly by using some of the code that you learned yourself:

 GetComponent(SpriteRenderer).sprite = Sprites[idx];