I don't recommend using that script it's very old.
I made some new scripts to deal with that.
This is a scriptable object you need to create one and assign it to the correct field when using the script below.
using UnityEngine;
namespace FreedTerror
{
[CreateAssetMenu(fileName = "New GameObject Info", menuName = "FreedTerror/U.F.E. 2 F.T.E./GameObject/GameObject Info", order = 1)]
public class UFE2FTEGameObjectInfo : ScriptableObject
{
public enum OrderInLayerMode
{
None,
Infront,
Behind,
Custom
}
public OrderInLayerMode orderInLayerMode;
public int orderInLayerInfront;
public int orderInLayerBehind;
public int orderInLayerCustom;
}
}
Place this script on a GameObject or Prefab it's a very nice utility script to cover various situations you may encounter.
using System;
using UnityEngine;
using UFE3D;
namespace FreedTerror
{
public class UFE2FTEGameObjectController : MonoBehaviour
{
#region Variables
private ControlsScript myControlsScript;
[SerializeField]
private bool useUpdate = true;
[SerializeField]
private UFE2FTEGameObjectInfo gameObjectInfo;
[Serializable]
private class AnimatorOptions
{
[HideInInspector]
public Animator myAnimator;
[HideInInspector]
public Animator[] childAnimators;
}
[Header("ANIMATOR OPTIONS (GAME OBJECT INFO REQUIRED)")]
[SerializeField]
private bool useAnimatorOptions;
[SerializeField]
private AnimatorOptions animatorOptions;
[Serializable]
private class ParticleSystemOptions
{
[Tooltip("DISABLES THE GAMEOBJECT IF THE PARTICLE SYSTEM IS NOT ALIVE.")]
public bool disableGameObjectIfNotAlive;
[HideInInspector]
public ParticleSystem myParticleSystem;
[HideInInspector]
public ParticleSystemRenderer myParticleSystemRenderer;
[HideInInspector]
public ParticleSystem[] childParticleSystems;
[HideInInspector]
public ParticleSystemRenderer[] childParticleSystemRenderers;
}
[Header("PARTICLE SYSTEM OPTIONS (GAME OBJECT INFO REQUIRED)")]
[SerializeField]
private bool useParticleSystemOptions;
[SerializeField]
private ParticleSystemOptions particleSystemOptions;
[Serializable]
private class SpriteRendererOptions
{
[HideInInspector]
public SpriteRenderer mySpriteRenderer;
[HideInInspector]
public SpriteRenderer[] childSpriteRenderers;
}
[Header("SPRITE RENDERER OPTIONS (GAME OBJECT INFO REQUIRED)")]
[SerializeField]
private bool useSpriteRendererOptions;
[SerializeField]
private SpriteRendererOptions spriteRendererOptions;
public enum GameObjectAction
{
Disable,
Enable
}
[Serializable]
private class CombatStanceOptions
{
public CombatStances[] combatStances;
public GameObjectAction gameObjectAction;
public GameObject[] gameObjects;
}
[Header("COMBAT STANCE OPTIONS")]
[SerializeField]
private bool useCombatStanceOptions;
[SerializeField]
private CombatStanceOptions[] combatStanceOptions;
[Serializable]
private class GameModeOptions
{
public GameMode[] gameModes;
public GameObjectAction gameObjectAction;
public GameObject[] gameObjects;
}
[Header("GAME MODE OPTIONS")]
[SerializeField]
private bool useGameModeOptions;
[SerializeField]
private GameModeOptions[] gameModeOptions;
[Serializable]
private class PlayerPausedOptions
{
public GameObjectAction gameObjectAction;
public GameObject[] gameObjects;
}
[Header("PLAYER PAUSED OPTIONS")]
[SerializeField]
private bool usePlayerPausedOptions;
[SerializeField]
private PlayerPausedOptions[] player1PausedOptions;
[SerializeField]
private PlayerPausedOptions[] player2PausedOptions;
#endregion
// Start is called before the first frame update
void Start()
{
if (useCombatStanceOptions == true)
{
myControlsScript = GetComponentInParent<ControlsScript>();
}
InitializeAnimatorOptions();
InitializeParticleSystemOptions();
InitializeSpriteRendererOptions();
UpdateCombatStanceOptions();
UpdateGameModeOptions();
UpdatePlayerPausedOptions();
}
// Update is called once per frame
void Update()
{
if (useUpdate == false) return;
UpdateAnimatorOptions();
UpdateParticleSystemOptions();
UpdateCombatStanceOptions();
UpdateGameModeOptions();
UpdatePlayerPausedOptions();
}
#region Animator Methods
private void InitializeAnimatorOptions()
{
if (useAnimatorOptions == false) return;
animatorOptions.myAnimator = GetComponent<Animator>();
animatorOptions.childAnimators = GetComponentsInChildren<Animator>();
}
private void UpdateAnimatorOptions()
{
if (useAnimatorOptions == false
|| gameObjectInfo == null) return;
if (UFE.isPaused() == false)
{
if (animatorOptions.myAnimator != null)
{
animatorOptions.myAnimator.enabled = true;
animatorOptions.myAnimator.speed = (float)UFE.timeScale;
}
int length = animatorOptions.childAnimators.Length;
for (int i = 0; i < length; i++)
{
if (animatorOptions.childAnimators[i] == null) continue;
animatorOptions.childAnimators[i].enabled = true;
animatorOptions.childAnimators[i].speed = (float)UFE.timeScale;
}
}
else
{
if (animatorOptions.myAnimator != null)
{
animatorOptions.myAnimator.enabled = false;
}
int length = animatorOptions.childAnimators.Length;
for (int i = 0; i < length; i++)
{
if (animatorOptions.childAnimators[i] == null) continue;
animatorOptions.childAnimators[i].enabled = false;
}
}
}
#endregion
#region Particle System Methods
private void InitializeParticleSystemOptions()
{
if (useParticleSystemOptions == false) return;
particleSystemOptions.myParticleSystem = GetComponent<ParticleSystem>();
particleSystemOptions.myParticleSystemRenderer = particleSystemOptions.myParticleSystem.GetComponent<ParticleSystemRenderer>();
particleSystemOptions.childParticleSystems = GetComponentsInChildren<ParticleSystem>();
int length = particleSystemOptions.childParticleSystems.Length;
particleSystemOptions.childParticleSystemRenderers = new ParticleSystemRenderer[length];
for (int i = 0; i < length; i++)
{
if (particleSystemOptions.childParticleSystems[i] == null) continue;
particleSystemOptions.childParticleSystemRenderers[i] = particleSystemOptions.childParticleSystems[i].GetComponent<ParticleSystemRenderer>();
}
}
private void UpdateParticleSystemOptions()
{
if (useParticleSystemOptions == false
|| gameObjectInfo == null) return;
if (particleSystemOptions.myParticleSystem != null
&& particleSystemOptions.disableGameObjectIfNotAlive == true
&& particleSystemOptions.myParticleSystem.IsAlive() == false)
{
this.gameObject.SetActive(false);
}
if (particleSystemOptions.myParticleSystem != null)
{
var myPS = particleSystemOptions.myParticleSystem.main;
myPS.simulationSpeed = (float)UFE.timeScale;
}
int length = particleSystemOptions.childParticleSystems.Length;
for (int i = 0; i < length; i++)
{
if (particleSystemOptions.childParticleSystems[i] == null) continue;
var childPS = particleSystemOptions.childParticleSystems[i].main;
childPS.simulationSpeed = (float)UFE.timeScale;
}
switch (gameObjectInfo.orderInLayerMode)
{
case UFE2FTEGameObjectInfo.OrderInLayerMode.Infront:
if (particleSystemOptions.myParticleSystemRenderer != null)
{
particleSystemOptions.myParticleSystemRenderer.sortingOrder = gameObjectInfo.orderInLayerInfront;
}
int length1 = particleSystemOptions.childParticleSystemRenderers.Length;
for (int i = 0; i < length1; i++)
{
if (particleSystemOptions.childParticleSystemRenderers == null) continue;
particleSystemOptions.childParticleSystemRenderers[i].sortingOrder = gameObjectInfo.orderInLayerInfront;
}
break;
case UFE2FTEGameObjectInfo.OrderInLayerMode.Behind:
if (particleSystemOptions.myParticleSystemRenderer != null)
{
particleSystemOptions.myParticleSystemRenderer.sortingOrder = gameObjectInfo.orderInLayerBehind;
}
int length2 = particleSystemOptions.childParticleSystemRenderers.Length;
for (int i = 0; i < length2; i++)
{
if (particleSystemOptions.childParticleSystemRenderers == null) continue;
particleSystemOptions.childParticleSystemRenderers[i].sortingOrder = gameObjectInfo.orderInLayerBehind;
}
break;
case UFE2FTEGameObjectInfo.OrderInLayerMode.Custom:
if (particleSystemOptions.myParticleSystemRenderer != null)
{
particleSystemOptions.myParticleSystemRenderer.sortingOrder = gameObjectInfo.orderInLayerCustom;
}
int length3 = particleSystemOptions.childParticleSystemRenderers.Length;
for (int i = 0; i < length3; i++)
{
if (particleSystemOptions.childParticleSystemRenderers == null) continue;
particleSystemOptions.childParticleSystemRenderers[i].sortingOrder = gameObjectInfo.orderInLayerCustom;
}
break;
}
}
#endregion
#region Sprite Renderer Methods
private void InitializeSpriteRendererOptions()
{
if (useSpriteRendererOptions == false) return;
spriteRendererOptions.mySpriteRenderer = GetComponent<SpriteRenderer>();
spriteRendererOptions.childSpriteRenderers = GetComponentsInChildren<SpriteRenderer>();
}
private void UpdateSpriteRendererOptions()
{
if (useSpriteRendererOptions == false
|| gameObjectInfo == null) return;
switch (gameObjectInfo.orderInLayerMode)
{
case UFE2FTEGameObjectInfo.OrderInLayerMode.Infront:
if (spriteRendererOptions.mySpriteRenderer != null)
{
spriteRendererOptions.mySpriteRenderer.sortingOrder = gameObjectInfo.orderInLayerInfront;
}
int length = spriteRendererOptions.childSpriteRenderers.Length;
for (int i = 0; i < length; i++)
{
if (spriteRendererOptions.childSpriteRenderers[i] == null) continue;
spriteRendererOptions.childSpriteRenderers[i].sortingOrder = gameObjectInfo.orderInLayerInfront;
}
break;
case UFE2FTEGameObjectInfo.OrderInLayerMode.Behind:
if (spriteRendererOptions.mySpriteRenderer != null)
{
spriteRendererOptions.mySpriteRenderer.sortingOrder = gameObjectInfo.orderInLayerBehind;
}
int length1 = spriteRendererOptions.childSpriteRenderers.Length;
for (int i = 0; i < length1; i++)
{
if (spriteRendererOptions.childSpriteRenderers[i] == null) continue;
spriteRendererOptions.childSpriteRenderers[i].sortingOrder = gameObjectInfo.orderInLayerBehind;
}
break;
case UFE2FTEGameObjectInfo.OrderInLayerMode.Custom:
if (spriteRendererOptions.mySpriteRenderer != null)
{
spriteRendererOptions.mySpriteRenderer.sortingOrder = gameObjectInfo.orderInLayerCustom;
}
int length2 = spriteRendererOptions.childSpriteRenderers.Length;
for (int i = 0; i < length2; i++)
{
if (spriteRendererOptions.childSpriteRenderers[i] == null) continue;
spriteRendererOptions.childSpriteRenderers[i].sortingOrder = gameObjectInfo.orderInLayerCustom;
}
break;
}
}
#endregion
#region Combat Stance Methods
private void UpdateCombatStanceOptions()
{
if (useCombatStanceOptions == false
|| myControlsScript == null) return;
int length = combatStanceOptions.Length;
for (int i = 0; i < length; i++)
{
int lengthA = combatStanceOptions[i].combatStances.Length;
for (int a = 0; a < lengthA; a++)
{
if (myControlsScript.currentCombatStance != combatStanceOptions[i].combatStances[a]) continue;
int lengthB = combatStanceOptions[i].gameObjects.Length;
for (int b = 0; b < lengthB; b++)
{
if (combatStanceOptions[i].gameObjects[b] == null) continue;
if (combatStanceOptions[i].gameObjectAction == GameObjectAction.Disable)
{
combatStanceOptions[i].gameObjects[b].SetActive(false);
}
else if (combatStanceOptions[i].gameObjectAction == GameObjectAction.Enable)
{
combatStanceOptions[i].gameObjects[b].SetActive(true);
}
}
}
}
}
#endregion
#region Game Mode Methods
private void UpdateGameModeOptions()
{
if (useGameModeOptions == false) return;
int length = gameModeOptions.Length;
for (int i = 0; i < length; i++)
{
int lengthA = gameModeOptions[i].gameModes.Length;
for (int a = 0; a < lengthA; a++)
{
if (UFE.gameMode != gameModeOptions[i].gameModes[a]) continue;
int lengthB = gameModeOptions[i].gameObjects.Length;
for (int b = 0; b < lengthB; b++)
{
if (gameModeOptions[i].gameObjects[b] == null) continue;
if (gameModeOptions[i].gameObjectAction == GameObjectAction.Disable)
{
gameModeOptions[i].gameObjects[b].SetActive(false);
}
else if (gameModeOptions[i].gameObjectAction == GameObjectAction.Enable)
{
gameModeOptions[i].gameObjects[b].SetActive(true);
}
}
}
}
}
#endregion
#region Player Paused Methods
private void UpdatePlayerPausedOptions()
{
if (usePlayerPausedOptions == false) return;
if (UFE2FTEPlayerPausedManager.playerPaused == UFE2FTEPlayerPausedManager.PlayerPaused.Player1Paused)
{
int length = player1PausedOptions.Length;
for (int i = 0; i < length; i++)
{
int lengthA = player1PausedOptions[i].gameObjects.Length;
for (int a = 0; a < lengthA; a++)
{
if (player1PausedOptions[i].gameObjects[a] == null) continue;
if (player1PausedOptions[i].gameObjectAction == GameObjectAction.Disable)
{
player1PausedOptions[i].gameObjects[a].SetActive(false);
}
else if (player1PausedOptions[i].gameObjectAction == GameObjectAction.Enable)
{
player1PausedOptions[i].gameObjects[a].SetActive(true);
}
}
}
}
else if (UFE2FTEPlayerPausedManager.playerPaused == UFE2FTEPlayerPausedManager.PlayerPaused.Player2Paused)
{
int length = player2PausedOptions.Length;
for (int i = 0; i < length; i++)
{
int lengthA = player2PausedOptions[i].gameObjects.Length;
for (int a = 0; a < lengthA; a++)
{
if (player2PausedOptions[i].gameObjects[a] == null) continue;
if (player2PausedOptions[i].gameObjectAction == GameObjectAction.Disable)
{
player2PausedOptions[i].gameObjects[a].SetActive(false);
}
else if (player2PausedOptions[i].gameObjectAction == GameObjectAction.Enable)
{
player2PausedOptions[i].gameObjects[a].SetActive(true);
}
}
}
}
}
#endregion
}
}