So, here I have a c# Unity script (for GPU instancing) where the mesh but also material variables seem to get destroyed. Weirdest part is that in the inspector everything seems to be fine, but every second update I get error ArgumentNullException: Value cannot be null. Parameter name: mesh.
Both mesh and material never get changed by any other script
Here is the script before using Resources.Load():
public class TestScript : MonoBehaviour
{
public int size = 10;
public int cubeSize = 10;
public Mesh mesh;
public Material material;
private Matrix4x4[] matrices;
private void Update()
{
matrices = new Matrix4x4[size * size * size];
int i = 0;
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size; y++)
{
for (int z = 0; z < size; z++)
{
Vector3 Pos = new Vector3(x*2, y*2+Mathf.Sin(Time.time + x + z), z*2);
matrices[i] = Matrix4x4.TRS(Pos, Quaternion.identity, Vector3.one);
i += 1;
}
}
}
//For Resources.Load() i use this:
//mesh = Resources.Load<Mesh>("Cube);
//material = Resources.Load<Material>("CubeMaterial");
Debug.Log(mesh);
Graphics.DrawMeshInstanced(mesh, 0, material, matrices);
}
}
[Error message and Debug.Log(mesh)]
(https://i.sstatic.net/4aGzf2cL.png)
I have tried putting material and mesh in Resources folder and using Resources.Load() function and it seems to be working.
For anyone wandering I made this question because I really wish to know why that happens. For now it seems to work just fine, but I want to avoid this if I will ever have to not use Resources folder.