I created a Global Functions script for functions I will need across multiple scripts and to be used in many instances (In this case, I created a function to recursively rename a component and all of its child components to a layer) however, I cannot figure out where I am going wrong with this. I have attached the script that creates the function here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GlobalFunctions : MonoBehaviour {
public static void SetLayerRecursively(GameObject obj, int newLayer)
{
//Debug.Log("Setting Layer");
obj.layer = newLayer;
foreach (Transform child in obj.transform)
{
SetLayerRecursively(child.gameObject, newLayer);
}
}
}
This is the function that I am having problems with. I call this function in 2 locations at the current moment. The first call I make, the function works as intended
using UnityEngine;
using UnityEngine.Networking;
public class PlayerSetup : NetworkBehaviour
{
[SerializeField]
Behaviour[] componentsToDisable;
[SerializeField]
string RemoteLayerName = "RemotePlayer";
[SerializeField]
string DontDrawLayerName = "DontDraw";
[SerializeField]
GameObject PlayerGraphics;
[SerializeField]
GameObject PlayerUIPrefab;
private GameObject PlayerUIInstance;
Camera sceneCamera;
void Start()
{
if (!isLocalPlayer)
{
DisableComponents();
AssignRemoteLayer();
}
else
{
sceneCamera = Camera.main;
if (sceneCamera != null)
{
sceneCamera.gameObject.SetActive(false);
}
GlobalFunctions.SetLayerRecursively(PlayerGraphics, LayerMask.NameToLayer(DontDrawLayerName));
PlayerUIInstance = Instantiate(PlayerUIPrefab);
PlayerUIInstance.name = PlayerUIPrefab.name;
}
GetComponent<PlayerManager>().Setup();
}
public override void OnStartClient()
{
base.OnStartClient();
string _NetID = GetComponent<NetworkIdentity>().netId.ToString();
PlayerManager _player = GetComponent<PlayerManager>();
GameManager.RegisterPlayer(_NetID, _player);
}
void AssignRemoteLayer()
{
gameObject.layer = LayerMask.NameToLayer(RemoteLayerName);
}
void DisableComponents()
{
for (int i = 0; i < componentsToDisable.Length; i++)
{
componentsToDisable*.enabled = false;*
}
}
void OnDisable()
{
Destroy(PlayerUIInstance);
if (sceneCamera != null)
{
sceneCamera.gameObject.SetActive(true);
}
GameManager.UnRegisterPlayer(transform.name);
}
}
However, when I call this function a second time, the layer does not get set.
using UnityEngine.Networking;
using System.Collections.Generic;
using UnityEngine;
public class PlayerShoot : NetworkBehaviour {
private const string PLAYER_TAG = “Player”;
[SerializeField]
private PlayerWeapon weapon;
[SerializeField]
GameObject WeaponGFX;
[SerializeField]
private string weaponLayerName = “Weapons”;
[SerializeField]
private Camera playerCam;
[SerializeField]
private LayerMask mask;
void start()
{
if (playerCam == null)
{
Debug.LogError(“PlayerShoot: No Camera Reference”);
enabled = false;
}
GlobalFunctions.SetLayerRecursively(WeaponGFX, LayerMask.NameToLayer(weaponLayerName));
}
void Update()
{
if (Input.GetButtonDown(“Fire1”))
{
Shoot();
}
}
[Client]
void Shoot()
{
RaycastHit _hit;
if (Physics.Raycast(playerCam.transform.position, playerCam.transform.forward, out _hit, weapon.range, mask))
{
if(_hit.collider.tag == PLAYER_TAG)
{
CmdPlayerShot(_hit.collider.name, weapon.damage);
}
}
}
[Command]
void CmdPlayerShot (string _playerID, int _damage)
{
Debug.Log(_playerID + " has been shot.");
PlayerManager _player = GameManager.GetPlayer(_playerID);
_player.RpcTakeDamage(_damage);
}
}
I am not sure exactly why this happens. Please take a look, I will upload any other assets you need to see if you ask. Thank you for your help!