GetComponent<MeshFilter> problem

Hey everyone. I’m trying to make a damage system do an object. I have 3 render meshes and 3 colliders meshes(Both MeshFilter and MeshCollider Components are in my object), but when I call them with this script:

switch(curMesh) {
		
	case possibleMeshes.undamaged:
		
		Mesh newMesh = damagedMeshes[1];
		GetComponent<MeshFilter>().mesh = newMesh;
		Mesh newCollider = damagedMeshesColliders[1];
		GetComponent<MeshCollider>().sharedMesh = newCollider;
		
		break;

}

Gives me an error in GetComponent.mesh = newMesh. The error is : ‘Object reference not set to an instance of an object’. I don’t know what to do anymore. Somebody can help me?

Thanks from now!

Do you actually have a MeshFilter attached to your gameObject? If not, that GetComponent will return null and you will get the Null Reference Exception that you've currently got, I believe. So add the MeshFilter component by selecting your gameObject and going Components > Mesh > Mesh Filter.

–

Are you sure it's the getComponent that's failing? Check newMesh, it's possible that that's where the problem is.

–

1 Answer

1

Problem solved!