fgm24/Assets/Scripts/Multiplayer/ZNGODisabler.cs

51 lines
1.4 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using Unity.Netcode.Components;
using UnityEngine;
/// <summary>
/// [Netcode for gameobjects] This is used for disabling components/objects depending on is host/server/client.
/// </summary>
public class ZNGODisabler : NetworkBehaviour
{
[SerializeField] private bool RunOnAwake = true;
[Space(10)]
[Tooltip("Add all the objects to disable on join")]
public UnityEngine.Object[] objectsToDisable;
private void Start() { if (RunOnAwake) ZDisable(); }
public void ZDisable()
{
if (RunOnAwake && IsOwner) return;
for (int i = 0; i < objectsToDisable.Length; i++)
{
var objectType = objectsToDisable[i]; // Apparently, "typeof" is not needed /shrug
switch (objectType)
{
case MonoBehaviour monoBehaviour:
monoBehaviour.enabled = false;
break;
case GameObject gameObject:
gameObject.SetActive(false);
break;
case Rigidbody rigidbody:
rigidbody.isKinematic = true;
break;
default:
Debug.LogError($"Can't disable: {objectType.name} (not supported)", objectType);
break;
}
}
}
}