Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RLin · Sep 06, 2015 at 10:53 PM · editordisableplaymode

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Sep 08, 2015 at 12:57 AM 0
Share

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.

avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RLin · Sep 07, 2015 at 08:07 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges