- Home /
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:
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
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 ();
}
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
Follow this Question
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