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 MantisMcKoy · May 18, 2016 at 09:15 AM · arraysspriterenderer

Array Index is out of Range and I don't understand why.

I'm not entirely sure what Code to include in this. Basically Im working with a deck of cards. I created a scene to create the deck and shuffle it and generally act as a deck, and it all works fine. But when I created a new scene and used the deck as a prefab I started getting an IndexOutOfRangeException.

 public void ToggleFace(bool showFace){
         if (showFace) {
                 spriteRenderer.sprite = faces[cardIndex];
             }
                                                 
         } else {
             spriteRenderer.sprite = cardBack;
         }
     }
 

After doing some debugging I found out the exception happens always on the first 'card' that is being created. To be honest It looks as though other than the exception being thrown it all runs. But Im guessing that somehow an instance from the faces array is being called before its created, but I dont understand why since the faces array is filled in the inspector. What should I do?

EDIT: In another script I have:

 void ShowCards(){
         int cardCount = 0;
         if (deck.HasCards) {
             int k = 0;
             foreach (Card i in deck.GetCards()) { 
                 float co = cardOffset * cardCount;
                 Vector3 temp = start + new Vector3 (co, 0f);
                 i.transform.position = temp;
 
                 if (i.getColorSuit () == ColorSuit.Blue) {
                     if (i.getBidAction () == BidAction.None) {
                         if (i.getCardValue () == 3) {
                             k = 20;
                         }
                         if (i.getCardValue () == 4) {
                             k = 22;
                         }
                         if (i.getCardValue () == 5) {
                             k = 7;
                         }
                     }
                     if (i.getBidAction () == BidAction.Getaway) {
                         k = 19;
                     }
                 }
                 i.cardIndex = k;
                 i.ToggleFace (true);                      
                 SpriteRenderer spriteRenderer = i.GetComponent<SpriteRenderer> ();
                 spriteRenderer.sortingOrder = deck.CardCount () - cardCount;
 
                 cardCount++;
 
             }
         }
     }

 

I cut a lot of the stuff for showing what causes k to be whatever number it is but you get the idea I think (k can be from 0-23 since that is how many card faces I have). And the error is pointing to where it gets called :

 void Update(){
         if (cardCounter != deck.CardCount ()) {
             cardCounter = deck.CardCount();
             ShowCards();
         }
     }


Comment
Add comment · Show 11
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
avatar image MantisMcKoy · May 18, 2016 at 09:17 AM 0
Share

Sorry if the code given is formatted in a weird way. On my screen it looks like a paragraph but when I try to edit it it looks as it should :/

avatar image Raresh · May 18, 2016 at 09:29 AM 0
Share

Without telling us where you assign a value to card index, there isn't much to do to help.

avatar image MantisMcKoy Raresh · May 18, 2016 at 09:43 AM 0
Share

Sorry I'm new to unity. I updated the post to have everything I think you'd need. But the short answer, I think, is its being assigned in the update method in another script.

avatar image meat5000 ♦ · May 18, 2016 at 09:47 AM 0
Share

In both your codeblocks you have unequal amounts of curly brackets.

Copy and paste the code from your code editor. Highlight all the code without anything non-code highlighted between and click the 101010 button.

Dont use whitespace (spacebar) for indentation in your code editor. Always use the tab key.

avatar image MantisMcKoy meat5000 ♦ · May 18, 2016 at 09:58 AM 0
Share

Thanks! I was totally doing that backwards

avatar image Nerevar · May 18, 2016 at 03:06 PM 0
Share

Hello, What is the exact line (in ShowCars() i guess?) where the exception is thrown?

avatar image MantisMcKoy Nerevar · May 18, 2016 at 08:22 PM 0
Share

In the show cards method the error is thrown at i.ToggleFace. Like I mentioned in the question when in its own scene the whole thing works as it should but now in another scene it doesn't. And the error always gets thrown on the first card instanciated

avatar image El__Nacho · May 18, 2016 at 08:36 PM 1
Share

Hm okay. Could you post the deck.CardCount() method? I think there could be the mistake because it probably counts the sprites in your faces array, right?

avatar image MantisMcKoy El__Nacho · May 19, 2016 at 02:03 AM 0
Share

I posted the method, but I did use a faces.Length to debug and possibly somewhere else. Could you please explain what the problem with that is?

Show more comments

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

44 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

Related Questions

[Unity2d Sprite Rendering Question] Grabbing and altering the sprite renderer of another game object 0 Answers

Array of Arrays 3 Answers

Array cannot read Greek characters 1 Answer

Randomize text position for 2D Quiz C# 0 Answers

how to change speed of animations in an array? 0 Answers


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