Leaving only one camera instance active/undestroyed at runtime?

Hi,

I’m working with a project that uses multiple cameras, but I only want one active at runtime depending on the build settings. Right now, my two cameras are attached to one parent ‘CameraSystem’ object, with a script with this function in it:

void Awake () {
		GameObject cameraOne = GetObjectByName ("Camera_1");
		GameObject cameraTwo = GetObjectByName ("Camera_2");
		#if CAM_ONE
		cameraOne.SetActive(true);
		cameraTwo.SetActive (false);
		Destroy(cameraTwo);
		cameraOne.tag = "MainCamera";
		#elif CAM_TWO
		cameraTwo.SetActive(true);
		cameraOne.SetActive (false);
		Destroy(cameraOne);
		cameraTwo.tag = "MainCamera";
		#endif
	}

I have Cloud Build set up to automatically change the scripting define symbols, so it’s not an issue with the code not running- I’ve confirmed this as working in the editor, and in my local builds. Maybe I’m doing something wrong?

EDIT: “GetObjectByName” is as follows:

static GameObject GetObjectByName(string name) {
		List<GameObject> objects = GetObjects ();
		foreach (GameObject obj in objects) {
			if (obj.name == name)
				return obj;
		}
		return null;
	}

static List<GameObject> GetObjects() {
		Object[] tempList = Resources.FindObjectsOfTypeAll (typeof(GameObject));
		GameObject temp;
		List<GameObject> realList = new List<GameObject> ();
		foreach (Object obj in tempList) {
			if (obj is GameObject) {
				temp = (GameObject)obj;
				if (temp.hideFlags == HideFlags.None)
					realList.Add ((GameObject)obj);
			}
		}
		return realList;
	}

To clarify, I have both cameras enabled by default. I should point out that I've tried various permutations of this- leaving one active as the 'default' camera only to reactivate the other one won't work, either.

Please describe what is NOT happening, that should. Or what is happening, that shouldn't.

1 Answer

1

I figured it out- the solution was to use DestroyImmediate() instead of just Destroy(). I’m not sure why this fixes it, but.

Ok. But can you create dump files if it is not crashing? Because freezing doesn't mean crashing in my case. The program works well, but the view is just freezing.