- Home /
Resources.load allways returns NULL but the Path seems to be correct
Hey Guys, I try to create a 2D Tilemap using prefabs for the Tiles.
But it seems like that the function cant find my prefab. Maybe you see what im doing wrong?
using UnityEngine;
using System.Collections;
public class roomController : MonoBehaviour {
GameObject[][] rooms;
public GameObject Troom;
int rows = 3;
int entrys = 4;
// Use this for initialization
void Start () {
Troom = Resources.Load<GameObject>("room");
for (int x = 0; x<rows; x++)
{
for (int y = 0; y<entrys; y++)
{
rooms[x][y] = Instantiate(Troom, new Vector3(x * 64, y * 64, 0), Quaternion.identity) as GameObject;
}
}
}
// Update is called once per frame
void Update () {
}
}
Thanx alot!
Answer by DiegoSLTS · Aug 11, 2016 at 02:30 PM
Can you show us what you see in the inspector when you select the "room" asset?
Also, I can imagine the answer for this, but can you share the error message you're getting or explain the incorrect behaviour you're seeing?
Anyway, from what you say I guess you're having a null pointer exception, but you have another problem in your script that's not related to the resource being loaded or not. Your "rooms" array is never initialized, so rooms[x][y] is not valid. Add this Awake method and see what happens:
void Awake() {
rooms = new GameObject[rows,entrys];
}
EDIT: Also, I have problems using [][] for 2D arrays. It works when I use [,], so maybe you also need to change:
GameObject[][] rooms;
to:
GameObject[,] rooms;
and:
rooms[x][y]...
to:
rooms[x,y]...