Your code seems fine, unless it can’t be ‘GameObject[ ]’ and needs to be ‘Object[ ]’.
For reference, here’s a part of my WorldManager class that simply loads all my levels (as TextAsset) (stored in “Assets/Resources/Levels/”)
public static class WorldManager {
public const string LevelPath = "Levels";
private static List<string> worldList;
public static void LoadWorldList() {
Object[] worlds = Resources.LoadAll(LevelPath, typeof(TextAsset));
// Make sure results aren't empty or null
if (worlds == null || worlds.Length == 0) {
// "#WorldManager.LoadWorldList() :: No World files found!"
}
// Add each result to the worldList
worldList = new List<string>();
foreach (Object world in worlds) {
TextAsset w = (TextAsset)world;
worldList.Add(w.name);
}
// Unload them - no longer needed
foreach (Object world in worlds) {
Resources.UnloadAsset(world);
}
}
Try adding the check for empty / null to see if you are getting any results.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class pertyMuch : MonoBehaviour {
public List<GameObject> prefabList = new List<GameObject>();
public int numberOfObjects;
public Transform player;
// public GameObject prefab1;
// public GameObject prefab2;
// public GameObject prefab3;
private Vector3 nextPosition;
// Use this for initialization
void Start () {
nextPosition = transform.localPosition;
numberOfObjects = 13;
GameObject[] gos = (GameObject[])Resources.LoadAll("Ground");
foreach(GameObject go in gos) {
prefabList.Add(go);
}
//prefabList.Add(prefab1);
//prefabList.Add(prefab2);
//prefabList.Add(prefab3);
}
// Update is called once per frame
void Update () {
int prefabIndex = UnityEngine.Random.Range(0, prefabList.Count);
for(int i = 0; i < numberOfObjects; i++) {
if(nextPosition.x > player.transform.position.x - 500){
GameObject o = (GameObject)Instantiate(prefabList[prefabIndex]);
o.transform.localPosition = nextPosition;
nextPosition.x -= o.transform.localScale.x;
}
}
GameObject[] gobs = GameObject.FindGameObjectsWithTag("Ground");
foreach(GameObject gob in gobs){
if(player.transform.position.x + 60 < gob.transform.position.x)
Destroy(gob);
}
}
}
The thing that may have affected it is the specification of the type. Easiest way to tell would be to try your old code again with the same extra param. Im thinking if this does work, the problem was the cast from Object[ ] to GameObject[ ] wasnt working
One thing is to cast an array of objects to the array of GameObjects, and another thing is to loop through elements (and cast each object to GameObject).
Am trying to achieve something similar, but I have to load all images from Resources/Textures… Which they all have format such as png, jpg and other format. After I load it then I have to draw each image with it texture.
Thanks
The error is saying your code is returning an asset that is not a valid Texture2D file.
Try putting in only one Texture2D file into that folder and try again.
This will make sure your code is at least valid.
Try different graphic files too, in case Unity doesn’t support certain formats.