- Home /
Scripts remain disabled after exiting the playmode?
I have this script that disables all cameras and then leaves only one enabled. Strangely, even when exiting the playmode, the other cameras remain disabled despite the fact that they are supposed to be enabled on scene start. I then am forced to manually enable of the affected cameras. Does anyone know why?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class EndChase : MonoBehaviour {
public Camera endChaseCam;
private Camera[] allCameras;
public Transform closestCop;
private List<Transform> CopsList = new List<Transform>();
// Use this for initialization
void Start () {
allCameras = Resources.FindObjectsOfTypeAll (typeof(Camera)) as Camera[];
for (int i = 0; i < allCameras.Length; i++) {
allCameras[i].gameObject.SetActive(false);
}
endChaseCam.gameObject.SetActive (true);
endChaseCam.enabled = true;
PoliceController[] allCops = Resources.FindObjectsOfTypeAll (typeof(PoliceController)) as PoliceController[];
for (int i = 0; i < allCops.Length; i++) {
if(allCops[i].gameObject.activeInHierarchy){
CopsList.Add(allCops[i].transform);
}
}
float copDistance = (transform.position - CopsList[0].transform.position).sqrMagnitude;
closestCop = CopsList [0];
for (int i = 0; i < CopsList.Count; i++) {
float dist =(transform.position - CopsList[i].position).sqrMagnitude;
if(dist <= copDistance){
copDistance = dist;
closestCop = CopsList[i];
}
}
}
// Update is called once per frame
void FixedUpdate () {
Vector3 rot = (closestCop.position - endChaseCam.transform.position);
if (rot != Vector3.zero) {
Quaternion quat = Quaternion.LookRotation (rot);
endChaseCam.transform.rotation = Quaternion.Lerp (endChaseCam.transform.rotation, quat, Time.fixedDeltaTime);
}
if (endChaseCam.fieldOfView > 30) {
endChaseCam.fieldOfView -= 10 * Time.fixedDeltaTime;
} else {
endChaseCam.fieldOfView = 30;
}
}
}
Edit: I tested this, and it appears to affect prefabs only. It appears that Unity saves the disabled state of the prefab, causing me to have to manually reactivate it. I cannot continue work on my project due to this issue. Anyone have a solution yet?
Answer by RLin · Sep 07, 2015 at 09:16 PM
Wow, I finally figured it out. Apparently, this line was problematic: allCameras = Resources.FindObjectsOfTypeAll (typeof(Camera)) as Camera[];
Not only would it result in the scene camera itself being disabled, but it would also cause the prefabs to keep their cameras disabled. I fixed it by changing the offending line to this: allCameras = Camera.allCameras;
Apparently, using FindObjectsOfTypeAll causes these problems.
Have you actually read the description of FindObjectsOfTypeAll as well as the warning?
This function can return any type of Unity object that is loaded, including game objects, prefabs, materials, meshes, textures, etc. It will also list internal stuff, therefore please be extra careful the way you handle the returned objects.
Answer by Suddoha · Sep 07, 2015 at 07:59 PM
As you stated, if you manipulate prefabs directly in your script, the changes will be saved as if you did it via the inspector. The solution is to either instantiate the prefabs and manipulate the clones or put them into the scene from the beginning.
The prefabs are in the scene from the scene start. Interestingly, I tried deactivating the offending script, then starting playmode and pausing the game and manually deactivating the prefab object while still in playmode, and upon exiting it would still be inactive. This is pretty much a roadblock in my development, and its extremely time consu$$anonymous$$g to have to go back and reactivate the 30 or so objects that are affected every single time.
Your answer
![](https://koobas.hobune.stream/wayback/20220612015214im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Singleton and MonoBehaviour in Editor 1 Answer
difficulty with InitializeOnLoad and Application.isPlaying 2 Answers
Draw line on play gets distorted 1 Answer
EditorLoop taking 30-40ms of every frame, any way to reduce this? 1 Answer
Stop MacOS system media playback when Unity enters Playmode 1 Answer