- Home /
Resources.load returns null
I've searched many other similar questions but none of the solutions to those helped me...
The problem is when i use Resources.load("myResource"); it always returns null.
The code(C#):
public class Fuser : MonoBehaviour {
public Mesh a;
public Mesh b;
public GameObject t;
void Start () {
a = Resources.Load("floor", typeof(Mesh)) as Mesh;
b = Resources.Load("floor") as Mesh;
//a.CombineMeshes(b);
//MeshFilter[] meshFilters = GetComponentsInChildren(MeshFilter);
CombineInstance[] combine = new CombineInstance[2];
//for ( i = 0; i < 2; i++){
combine[0].mesh = a;
combine[0].transform = new Matrix4x4();
combine[0].mesh = b;
Matrix4x4 m = new Matrix4x4();
m.SetTRS(new Vector3(0,2,0), Quaternion.identity, Vector3.up);
combine[0].transform = m;//new Vector3(0,2,0);//a.transform.localToWorldMatrix;
//meshFilters[i].gameObject.active = false;
//}
t.GetComponent<MeshFilter>().mesh = new Mesh();
t.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
//transform.gameObject.active = true;
}
// Update is called once per frame
void Update () {
}
}
And the project structure:
I have tried many different ways and names but none seems to work... I don't know what i am doing wrong...
Sorry, but which line in your code is returning a null reference exception? Just after you load 'floor', have you tried checking whether a and b are null? (e.g. if(a == null) Debug.Log("A is null");
I suspect that the file you're loading may not be a mesh - perhaps it's rather something containing a mesh.
Do you also get a null return value if you simply try to store it in a variable of type Object? If it's an object of type GameObject, you may have to call .renderer.mesh on it in order to obtain the mesh.
Yes, it was the whole 'as $$anonymous$$esh' and it was loading a GameObject... Funny how it returns null ins$$anonymous$$d of a error... I would never have guessed that one, thanks.
How do i mark as this being the answer?
BTW: The fact that it returns 'null' is one of the effects of casting with 'as' (and the reason to use it) - it's often desirable to return a null rather than throw an error. Alternative ways of casting, for instance using (GameObject), will throw an error.
Unfortunately, you can't mark a comment as being an answer, but if Bernard will convert his comment to an answer you can mark it.
Answer by numberkruncher · Jan 21, 2013 at 01:08 AM
When meshes are imported into Unity, a game object is added for the root object and then one for each sub-mesh. In your case you are attempting to access the container object as opposed to the mesh.
You can use the following method to access the mesh(es) in your asset:
using System.Linq;
...
GameObject floorModel = Resources.Load(
"floor", typeof(GameObject)
) as GameObject;
MeshFilter[] meshFilters =
(MeshFilter[])floorModel.GetComponentsInChildren(
typeof(MeshFilter)
);
Mesh[] meshes = (Mesh[])meshFilters.Select(
filter => filter.sharedMesh
);
Your answer
Follow this Question
Related Questions
Resources.load returns null instead of AudioClip 1 Answer
How do I make sure a resource is loaded or is instantiated? 2 Answers
load resources material 0 Answers
Accessing 4.6 GUI build-in sprites in C# 0 Answers
Resources.Load wont read text file 6 Answers