SetActive Stopped Working After Script Rename

Everything in this script was working perfectly. Then I decided to change the script name (I also changed the class name to the new name). After doing that, it no longer works reliably. I’ve restarted Unity, removed the script and re-attached the GameObjects, etc.

The funny thing is that while running, part of the script thinks that the other game object has become active. You can even see in the project view that the other object is becoming active, while one is becoming inactive as it should. It’s acting extremely weird now.

The code in Awake is no longer working to decide which ship is in view first at the start. Even though it was before I changed the script name. If I manually enable/disable one ship for the other before starting the game, all relevant camera switching code that relies on one or the other ship being active works as it should. Though when you press S to change the ship, it thinks it changed and then it will use camera views for the ship it was supposed to change to. It makes no sense. It’s checking to see which one is active when it decides which camera views to use. So Unity thinks the right object is active, but it’s not.

https://hastebin.com/gonuhikaza.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ViewChange : MonoBehaviour {

    public Material m_Skybox1;
    public Material m_Skybox2;
    public Material m_Skybox3;
    public Material m_Skybox4;
    public Material m_Skybox5;
    public Material m_Skybox6;

    public Camera c_Cam1;
    public Camera c_Cam2;
    public Camera c_Cam3;
    public Camera c_Cam4;

    public GameObject s_XWing;
    public GameObject s_TIE;

    AudioSource snd_Click1;
    AudioSource snd_Click2;
    AudioSource snd_Click3;

    private void Awake()
    {

        s_XWing.SetActive(true);
        s_TIE.SetActive(false);
     
    }


    // Use this for initialization
    void Start () {

        // Start w/ CAM 1
        c_Cam1.enabled = true;
        c_Cam2.enabled = false;
        c_Cam3.enabled = false;
        c_Cam4.enabled = false;    

        AudioSource[] audios = GetComponents<AudioSource>();
        snd_Click1 = audios[0];
        snd_Click2 = audios[1];
        snd_Click3 = audios[2];

    }
 
    // Update is called once per frame
    void Update () {

        // 3D MODEL TOGGLE
        if (Input.GetKeyDown(KeyCode.S) && (s_XWing.activeSelf))
        {
            snd_Click3.Play();
            s_XWing.SetActive(false);
            s_TIE.SetActive(true);
            c_Cam1.enabled = true;
        }

        else if (Input.GetKeyDown(KeyCode.S) && (s_TIE.activeSelf))
        {
            snd_Click3.Play();
            s_XWing.SetActive(true);
            s_TIE.SetActive(false);
            c_Cam1.enabled = true;
        }


        // Skybox Switches
        if (Input.GetKeyDown(KeyCode.Alpha1)) { snd_Click2.Play(); RenderSettings.skybox = m_Skybox1; }
         
        if (Input.GetKeyDown(KeyCode.Alpha2)) { snd_Click2.Play(); RenderSettings.skybox = m_Skybox2; }          

        if (Input.GetKeyDown(KeyCode.Alpha3)) { snd_Click2.Play(); RenderSettings.skybox = m_Skybox3; }
         
        if (Input.GetKeyDown(KeyCode.Alpha4)) { snd_Click2.Play(); RenderSettings.skybox = m_Skybox4; }
         
        if (Input.GetKeyDown(KeyCode.Alpha5)) { snd_Click2.Play(); RenderSettings.skybox = m_Skybox5; }
         
        if (Input.GetKeyDown(KeyCode.Alpha6)) { snd_Click2.Play(); RenderSettings.skybox = m_Skybox6; }
         

        // CAM MODE TOGGLE
        if (Input.GetKeyDown(KeyCode.C) && (c_Cam1.enabled == true))
        {
            snd_Click1.Play();
            c_Cam1.enabled = false;
            c_Cam2.enabled = true;
            c_Cam3.enabled = false;
            c_Cam4.enabled = false;
        }
        else if (Input.GetKeyDown(KeyCode.C) && (c_Cam2.enabled == true))
        {
            snd_Click1.Play();
            c_Cam1.enabled = false;
            c_Cam2.enabled = false;
            c_Cam3.enabled = true;
            c_Cam4.enabled = false;
        }
        else if (Input.GetKeyDown(KeyCode.C) && (c_Cam3.enabled == true)
            && (s_XWing.activeSelf))
        {
            snd_Click1.Play();
            c_Cam1.enabled = true;
            c_Cam2.enabled = false;
            c_Cam3.enabled = false;
            c_Cam4.enabled = false;
        }

        // Enter TIE Cockpit View
        else if (Input.GetKeyDown(KeyCode.C) && (c_Cam3.enabled == true)
            && (s_TIE.activeSelf))
        {
            snd_Click1.Play();
            c_Cam1.enabled = false;
            c_Cam2.enabled = false;
            c_Cam3.enabled = false;
            c_Cam4.enabled = true;
        }

        // Exit TIE cockpit view
        else if (Input.GetKeyDown(KeyCode.C) && (c_Cam4.enabled == true))
        {
            snd_Click1.Play();          
            c_Cam1.enabled = true;
            c_Cam2.enabled = false;
            c_Cam3.enabled = false;
            c_Cam4.enabled = false;
        }

    }
}

Your classname (in this case ViewChange ) and the name of the file where the class is in, have the be the same.
So calling the file that houses the class “ViewChange” “gonuhikaza.cs” does not work.

It’s the same.

No errors and most of it works minus SetActive portions.

The name of your script must be ViewChange.cs.

Your problem proves, if needed, that changing the name of a script is something to avoid. Create a new script by the name ViewChange.cs and then copy and paste the content of the broken script without changing anything in it, especially the name of the class.

It is.

Okay, then do what I say in my previous post, that I edited while you were answering.

Someone recommend on IRC that I do that and I tried and did it again just now. Once with a different file name/class name. Went back to the original name this time. Same behavior with both methods.

Do not modify class names or scripts’ names; remake them, it’s safer.

Yea, I don’t know. It’s acting very weird. I just changed the two GameObjects in inspector to the same ship and it actually shows the wrong on one screen even then. It seems to depend on what I personally enable in inspector for what shows up. If I disable them both in inspector nothing does as expected. It’s entirely relying on what is active hierarchy/inspector and ignoring SetActive scripts since it started to behave this way.

Setup as it should be it still switches to the fourth camera when toggle to what should be the other ship and doesn’t toggle to the fourth camera when the first ship should be showing as if it thinks the correct one is active. If you look at the models in the model folder, when you toggle between the ships you’ll see the models change accordingly.

So weird.