- Home /
The question is answered, right answer was accepted
loading prefabs in an array
hi need to load all the prefabs in the resources folder into an array. the script is below. the problem is when i run the script it shows an error message as below. i dont know where am making mistake. need help on how to load the prefabs.
NullReferenceException: Object reference not set to an instance of an object runtime.Start () (at Assets/scripts/runtime.cs:22)
using UnityEngine;
using System.Collections;
public class runtime : MonoBehaviour {
public int rows;
public int columns;
public int n = 3;
int initialPositionX = -5;
int initialPositionY = 5;
float bufferSpace = 1.5f;
public Transform prefab;
public GameObject[] items;
// Use this for initialization
void Start () {
items = new GameObject[];
items = Resources.LoadAll("prefabs")as GameObject[];
//Debug.Log(items.Length);
int index = Random.Range(0, items.Length);
cardCreation(index);
}
// Update is called once per frame
void Update () {
}
public void cardCreation(int cardIndex)
{
float spaceBetweenCards = prefab.transform.localScale.x;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
Instantiate(items[cardIndex], new Vector3((j*bufferSpace+spaceBetweenCards),(i*bufferSpace+spaceBetweenCards),0),
Quaternion.Euler(270,0,0));
}
}
}
}
Answer by HarshadK · Sep 15, 2014 at 11:27 AM
In your this line
items = new GameObject[];
your array is not initialized. You should actually get an error stating:
CS1586: Array creation must have array size or array initializer
So you can set the length of your array dynamically as:
items = new GameObject[Resources.LoadAll<GameObject>("prefabs").Length];
Or you can also specify the value for length there directly.
And you populate it using:
items = Resources.LoadAll<GameObject>("prefabs")as GameObject[];
Also remember there should be a folder names 'prefabs' inside your Resources folder where all your prefabs will be stored. This 'prefabs' is the argument you pass to your Resources.LoadAll method.
when i do like this items = new GameObject[Resources.LoadAll("prefabs")as GameObject[]];
i get an error message stating Assets/scripts/runtime.cs(19,80): error CS0029: Cannot implicitly convert type UnityEngine.GameObject[]' to
int'
It is:
Resources.LoadAll<GameObject>("prefabs").Length
that you put inside those square brackets.
done working. but another error message stating
ArgumentException: The prefab you want to instantiate is null.
i have a folder named as prefab already. the path is correct.
i have a folder named as prefab already
but the argument has prefabs as the name.
Check for the spelling of the folder name.
i just had to say thank you for your answer, this was doing my head in for over an hour before i stumbled upon this. i initially tried doing this manually and loading each asset in independandtlyusing
string[] directories = Directory.GetDirectories("Assets/Resources/Database");
gaining the directories of all my prefabs worked perfectly fine. its this next step.
which i put this in a loop
List<GameObject> prefabsList = new List<gameObject>();
foreach (string dir in directories)
{
GameObject[] prefabs = Resources.LoadAll(dir) as GameObject[];
for(int i = 0; i < prefabs.Length; i ++)
{
prefabsList.Add(prefabs[i]);
}
}
however i was more than sure this method would have worked but it seemed to throw an error with the loading of resources.
anyways enough yapping, i just wanted to say thank you for your answer as this really helped.
Answer by Cherno · Sep 15, 2014 at 11:12 AM
It's possible, depending on the number of Prefabs that have to be loaded, that not all could be loaded into the array before cardCreation starts to try and access prefabs from the array. Maybe try starting cardCreation as a Coroutine with a 1-second delay or something. In anyway, if you don't call cardCreation at all, is the array successfully filled with Prefabs?
actually the array is not getting loaded. i manually connected the prefabs in the inspector, but when running the script the array shows zero elements. trying to figure out wat is the problem.
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
NPCs follow player in a line 1 Answer
Enemy frozen in place by light? 2 Answers
Coordinating multiple AI enemies 3 Answers
How to make basic AI in a 2d game? 4 Answers