not set to an instance .... problem

Hello guys,

when I run this function the function in the script I get an error which is "NullReferenceException: Object Reference not set to an instance of an object.But I can’t figure out where this error occurs.
I can see that the function works well to the GameObject.Find(“Room”) call but afterwards it stucks there. But I don’t understand why as “Room” is a valid object instantiated properly (I guess) and it corresponds a MeshFilter. Any idea if something else can generate that error.?

Many Thanks.

function SetIsometricView() {
	camera.transform.LookAt(target);
	camera.orthographic = true;
	
	
	var gg : GameObject = GameObject.Find("Room"); //There is only 1 Room
	Debug.Log("SetIso View: gg : " + gg);
	var roomBounds : Bounds = gg.GetComponent(MeshFilter).mesh.bounds;	
	Debug.Log("Bounds:: " + roomBounds);

	ortho =  true;
	
	dimension = Mathf.Max( Mathf.Max(Mathf.Abs(roomBounds.extents.x), Mathf.Abs(roomBounds.extents.y)), Mathf.Abs(roomBounds.extents.z));

	camera.transform.position = Vector3(
		Mathf.Abs(dimension), 
		Mathf.Abs(dimension) ,
		Mathf.Abs(dimension)
	);

	camera.transform.LookAt(roomBounds.center);
	camera.orthographicSize =Mathf.Max( Mathf.Max(Mathf.Abs(roomBounds.extents.x), Mathf.Abs(roomBounds.extents.y)), Mathf.Abs(roomBounds.extents.z));
	camera.farClipPlane = 3000;
	//camera.nearClipPlane = 2;
	
};

Are you sure your “Room” game object is not null?? then try to use camera.main instead of camera and sat your camera to main camera from editor.

Actually I am investigating on it and Room is of type UnityEngine.GameObject. So, I guess it’s not null.
Room is a Prefab I load, and it has a sup component called “default” which contains the meshfilter… but for whatever reason I can’t get to access that field :frowning: so If I call Room.GetComponent(“meshFilter”) I get the error.

so how can I access the “default” component as I tryed Room.GetComponent(“default”) but still having troubles.

Try to get the mesh filter using,

var mf : MeshFilter = Room.GetComponent(MeshFilter) as MeshFilter;

before it check if your room is null or not. You can use Debug.Log(Room); to see it