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;
}
}
}
