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));
}
}
}
}
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
– galaboyUnityEngine.GameObject[]' toint'It is: Resources.LoadAll<GameObject>("prefabs").Length that you put inside those square brackets.
– HarshadKdone 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.
– galaboy> i have a folder named as prefab > already but the argument has prefabs as the name. Check for the spelling of the folder name.
– HarshadK