Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Troubleshoot a script that generates cards with random images

Discussion in 'Scripting' started by agostonr, Jul 25, 2016.

  1. agostonr

    agostonr

    Joined:
    Jan 3, 2016
    Posts:
    193
    Greetings All,
    I have been working on this and it is giving me trouble. The first issue is that no matter how others' said on these forums, I can't make Resources.LoadAll work they way it should. What's more, I don't know if my script would actually work.
    I created some linked lists with functions like indexing (though not operator overload), delete the element of the given number and so on, that WORKS (I did test it out). I don't include it here to spare space. The issue is with the other parts.
    EDIT: THE ISSUE:
    The Resources.LoadAll function just does not load anything into my array, thus the code return with a null ref exception.


    Code (CSharp):
    1. public sealed class AllMySprites : MonoBehaviour
    2. {
    3.     public static int howManyBrotherSprites; //meaning how many do we have of the same sprite in the game.
    4.     public static MyLinkedList<Sprite> cardSprites;
    5.     public static Sprite[] allMySprites;
    6.     //need this because we will have 2 of them.
    7.     public static void InitializeMySprites()
    8.     {
    9.         howManyBrotherSprites = 2;
    10.         //loading all the images
    11.         allMySprites = Resources.LoadAll<Sprite>("Cards");
    12.         //giving the desired size to the container of my sprites.
    13.         cardSprites = new MyLinkedList<Sprite>();
    14.         //now assigning all of them to the 2d array. meaning I have e.g. 2 list of my images.
    15.         int indexForCards = 0;
    16.         for (int j = 0; j < howManyBrotherSprites; j++)
    17.         {
    18.             for (int i = 0; i < allMySprites.Length; i++, indexForCards++)
    19.             {
    20.                 cardSprites.AddToTheList(allMySprites[i]);
    21.             }
    22.         }
    23.         //after this we should have all the cards in the cardSprites, and the desired number of them.
    24.         //notice that we don't have to instantiate an AllMySprites variable - we can't even, actually as it is sealed.
    25.         //the purpose is that at the beginning of the game we can assign a sprite to each card we summoned.
    26.      
    27.     }
    28. }
    29. public class OneCard : MonoBehaviour
    30. {
    31.     //public Component texture; //the sprite of the card
    32.     public bool isTurnedDown; //meaning the picture on it is hidden
    33.     public Sprite turnedDownSprite;
    34.     public Sprite actualImageSprite;
    35.     int currentIndex = 0;
    36.     public OneCard()
    37.     {
    38.  
    39.         turnedDownSprite = Resources.Load("empty", typeof(Sprite)) as Sprite;
    40.         isTurnedDown = true;
    41.         //getting a random index to assign to the card.
    42.         currentIndex = AllMySprites.cardSprites.Length * AllMySprites.howManyBrotherSprites;
    43.  
    44.         if(AllMySprites.cardSprites.FindGivenElementIfExists(currentIndex) != null)
    45.         {
    46.             //if the sprite isn't already used up
    47.             actualImageSprite = AllMySprites.cardSprites.FindGivenElementIfExists(currentIndex);
    48.             //then we use it up.
    49.         }
    50.         //note that using linked lists has a benefit. As we use up an element, we delete it and the list adapts dynamically,
    51.         //so there is no need to mess around with the empty array elements.
    52.         AllMySprites.cardSprites.DeleteGivenElement(currentIndex);
    53.      
    54.     }
    55.  
    56. }
    57. public class GameLogic : MonoBehaviour
    58. {
    59.     static int size = 4;
    60.     static OneCard[,] blockOfCards = new OneCard[size, size];
    61.     // Use this for initialization
    62.     void Start()
    63.     {
    64.         AllMySprites.InitializeMySprites();
    65.         //we have to instatiate a couple of cards here. The number must be a power of two and it is determined by
    66.         //the difficulty of the game, more cards mean more to keep in mind.
    67.         for(int i = 0; i < size; i++)
    68.         {
    69.             for(int j = 0; j < size; j++)
    70.             {
    71.                 blockOfCards[i, j] = new OneCard();
    72.                 Instantiate(blockOfCards[i, j], new Vector3(i * 5, j * 5, 0), Quaternion.identity);
    73.             }
    74.         }
    75.  
    76.     }
    77.  
    78.     // Update is called once per frame
    79.     void Update ()
    80.     {
    81.      
    82.     }
    83. }
     
    Last edited: Jul 25, 2016
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    what does it do/not do?, errors?, information...

    don't use linked lists, generic lists function much better
     
    Last edited: Jul 25, 2016
    agostonr likes this.
  3. agostonr

    agostonr

    Joined:
    Jan 3, 2016
    Posts:
    193
    Heh right there you are, I forgot to post the issue.
    The other thing, you must be right, but I know how this linked list works and it comes in handy. btw my linked list is generic as well. Again, that part is functioning, I tested it separately and in this context both. The main issue is with the Resources.LoadAll, it does not load anything into my array.
    I have the folder like this in my assets:
    Assets/Cards, in that I have all my images as sprites.
     
  4. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    Try Assets/Resources/Cards
    It returns null because it doesn't find the folder called Resources.
     
    agostonr likes this.
  5. agostonr

    agostonr

    Joined:
    Jan 3, 2016
    Posts:
    193
    Thanks, it works, but now I get another null ref, at my line 72. Do you have any ideas?
     
  6. agostonr

    agostonr

    Joined:
    Jan 3, 2016
    Posts:
    193
    Uhh, and that random index, well that ain't so random. Took a break and forgot to add it there.
    Now it is random, still the same error.
    And in line 71, I get a warning that I can't use the "new" keyword when adding a MonoBehaviour descendant. Need to use the AddComponent instead. How on Earth shall I use that add component here??
     
    Last edited: Jul 25, 2016
  7. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    If OneCard is a MonoBehaviour, then it shouldn't be created with new - Unity is probably giving an error about that, in fact.

    You'd want something like (inside the loop):
    Code (csharp):
    1. GameObject go = new GameObject();
    2. go.transform.position = new Vector3(i * 5, j * 5, 0);
    3. blockOfCards[i, j] = go.AddComponent<OneCard>();
     
    agostonr likes this.
  8. agostonr

    agostonr

    Joined:
    Jan 3, 2016
    Posts:
    193
    See, all of you spoke wisdom and all the cards appear twice, just as I wanted to.
    Now I only need to make them appear in my viewport. Any ideas?
     
  9. agostonr

    agostonr

    Joined:
    Jan 3, 2016
    Posts:
    193
    Sod it, just needed a sprite renderer. Now it works the way I want it to. Thanks to all :)