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 /
avatar image
0
Question by shepsaus000 · May 18, 2016 at 07:05 PM · scripting problemspritestutorialfolderresources.loadall

Having trouble with LoadAll

I've been following a tutorial to figure out how to make a puzzle game: https://www.youtube.com/watch?v=2ii0rPoiGkw

My problem is in the first three minutes and it involves the LoadAll bit of his script. You see, unlike the guy in the tutorial, I'm using a folder for Candy whereas he's using... something. I actually can't quite tell what he's using by the icon that is shown (I'm still a bit new to Unity I guess :P). The exact icon for what Candy is is found at 1:00.

Anyway, due to my using a folder instead of... whatever he's using, I'm getting an error. To say what exactly is going on, I'll show you my script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;
 
 public class GameController : MonoBehaviour {
 
     public Sprite bgImage;
 
     public List<Button> btns = new List<Button> ();
 
     public Sprite[] puzzles;
 
     public List<Sprite> gamePuzzles = new List<Sprite>();
 
     private bool firstGuess, secondGuess = true;
 
     private int countGuesses;
     private int countCorrectGuesses;
     private int gameGuesses;
 
     private int firstGuessIndex, secondGuessIndex;
 
     private string firstGuessPuzzle, secondGuessPuzzle;
 
     void Awake (){
         puzzles = Resources.LoadAll<Sprite> ("Sprites/Candy");
     }
 
 
 
     // Use this for initialization
     void Start () {
         GetButtons ();
         AddListeners ();
         AddGamePuzzles ();
         gameGuesses = gamePuzzles.Count / 2;
         Shuffle (gamePuzzles);
     }
     
     // Update is called once per frame
     void GetButtons () {
         GameObject[] objects = GameObject.FindGameObjectsWithTag ("PuzzleButton");
             for (int i = 0; i < objects.Length; i++) {
             btns.Add(objects [i].GetComponent<Button> ());
             btns [i].image.sprite = bgImage;;
         }
     }
 
     void AddGamePuzzles (){
         int looper = btns.Count;
         int index = 0;
 
         for(int i = 0; i < looper; i++){
             if (index == looper / 2) {
                 index = 0;
             }
 
             gamePuzzles.Add (puzzles [index]);
 


Don't mind the parsing errors. I just want to show what is necessary. Anyway, I'm getting an error regarding the last line of code. It says the following: "Array index is out of range."

I know that this error is regarding the Resources.LoadAll bit, because that is the ONLY time in the entire tutorial that I had a slightly different situation before me, and that situation is that I have a folder for "Candy" rather than... whatever that guy has. Is there some way to fix this, or am I utterly doomed D:

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by TBruce · May 18, 2016 at 07:28 PM

The namespace functions like Load and LoadAll require that the resources in question be under a folder named Resources. His project folder is not set up correctly. For his code and your code to work properly you need to create a Resources folder and place your Sprites folder inside it. Like this

alt text

Also, inside "AddGamePuzzles" index is alway 0. So your code will always be doing this

 gamePuzzles.Add (puzzles [0]);

In Start() you may want to do this

 GetButtons ();
 if ((puzzles != null) && (puzzles.Length > 0) && (btns.Count > 0))
 {
     AddListeners ();
     AddGamePuzzles ();
 }



inspector.png (10.0 kB)
Comment
Add comment · Show 1 · Share
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 shepsaus000 · May 19, 2016 at 05:31 PM 0
Share

Alright, so this got rid of the error, but all the various sprites that I have in my "Sprites" folder under Resources don't load. This is because my images are 2D textures. How do I change that to be a sprite? I thought simply changing the texture type to a sprite would work but I guess not :/

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

51 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

Related Questions

Ref to Texture Missing when Creating Sprite by Script during Asset Postprocessing 1 Answer

In-Game Tutorial with special actions in special areas. (Shooter) 0 Answers

Is it possible and practical to make 2d game with Unity? 1 Answer

Script I copied from Movement tutorial doesnt work 2 Answers

How to minimize directional changes of A* autopathing movement? 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