Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by tmcd35 · Mar 16, 2017 at 08:33 PM · spritecanvasimagearraysresources

Loading a new sprite into an image array

Hi,

I'm trying to produce a basic BlackJack card game. I have a Resources folder containing a border image, a card back image, and a spliced image of 52 cards named cards_0...cards_52.

I have a single gameobject called GameSystem with a single C# script attached to control the entire game.

I have these two lines to use the Inspectors drag n drop method to associate the card image display in the canvas with the GameSystem.cs script:

     public Image[] playerCards = new Image[5];                            
     public Image[] dealerCards = new Image[5];                

I have a new class to hold the properties for each card:

     public class PlayingCard {
         public string card;    
         public bool used;        
         public int value;    
     
         //Constructor for PlayingCard object
         public PlayingCard (string cardSprite, int faceValue) {
             card = cardSprite;            
             used = false;                    
             value = faceValue;    
         }
     }

of which I've made an array of card objects:

 private PlayingCard[] deck = new PlayingCard[52];    

The cards are initiated with the name of the appropriate card sprite held in the PlayingCard object card string (card_0...card_52):

 string cardSprite = "cards_" + cardNumber.ToString ();
 deck[cardNumber] = new PlayingCard (cardSprite, Mathf.Clamp (card + 1, 1, 10));

So, later on, in the actual game, I'm randomly chosing which card to display

 int[] chosenCard = new int[4];                            
         
         //deal four cards
         for (int deal = 0; deal < 4; deal = deal + 1) {
             bool validCard = false;                        
             
             //repeat until valid card is chosen
             while (validCard == false) {
                 int randomCard = Random.Range (1, 52)
                 
                 //check if card is valid
                 if (deck[randomCard].used == false) {
                     chosenCard[deal] = randomCard;
                     deck[randomCard].used = true;
                     validCard = true;
                 }
             }    
         }

Once the cards have been chosen, I'm trying to update the sprite image in the canvas with the sprite image of the chosen cards with the following code:

 dealerCards[0].sprite = Resources.Load (deck[chosenCard[0]].card) as Sprite;
 dealerCards[1].sprite = Resources.Load ("CardBack") as Sprite;
 playerCards[0].sprite = Resources.Load (deck[chosenCard[2]].card) as Sprite;
 playerCards[1].sprite = Resources.Load (deck[chosenCard[3]].card) as Sprite;

However, the images don't change, the remain with the empty image place holders.

Can someone point me in the direction of where I'm going wrong?

playerCards[n] is an image and is associated in the inspector with the correct canvas image placeholder object. playerCards[n].sprite should change the sprite image associated with that canvas image object. Resources.Load should load the named sprite from the resources folder. deck[chosenCard[n].card] is returning the correct sprite name string, eg cards_13. and it told to load "as Sprite".

Why is this not working?

Thanks in advance for any pointers.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

119 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Background in 2D game: sprite or image? 0 Answers

Resource.Load always returning null when trying to load an image 1 Answer

Generating a texture and using as Source Image 0 Answers

How to resize an image, without affecting the size of the button? 0 Answers

How can I make Image border not change a size? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges