- Home /
Game Object Layer changes automatically on runtime.,Game Object's Layer automatically changes in runtime?
I set a prefab object known as "Armor" and set its layer to "default"
I click the play button...
The "Armor" layer then changes to "weapon" instead of default
Question: Why does the armor's layer changes on runtime? I want to keep it the layer as it is and not change it during runtime.
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class WeaponManager : NetworkBehaviour {
[SerializeField]
private string weaponLayerName = "Weapon";
[SerializeField]
private Transform weaponHolder;
[SerializeField]
private PlayerWeapon primaryWeapon;
private PlayerWeapon currentWeapon;
private WeaponGraphics currentGraphics;
public bool isReloading = false;
void Start ()
{
EquipWeapon(primaryWeapon);
weaponLayerName = "Default";
}
public PlayerWeapon GetCurrentWeapon ()
{
return currentWeapon;
}
public WeaponGraphics GetCurrentGraphics()
{
return currentGraphics;
}
void EquipWeapon (PlayerWeapon _weapon)
{
currentWeapon = _weapon;
GameObject _weaponIns = (GameObject)Instantiate(_weapon.graphics, weaponHolder.position, weaponHolder.rotation);
_weaponIns.transform.SetParent(weaponHolder);
currentGraphics = _weaponIns.GetComponent<WeaponGraphics>();
if (currentGraphics == null)
Debug.LogError("No WeaponGraphics component on the weapon object: " + _weaponIns.name);
if (isLocalPlayer)
Util.SetLayerRecursively(_weaponIns, LayerMask.NameToLayer(weaponLayerName));
}
}
,Ok so the I set a game object's layer (as prefab) to default. Then when I tested the game during runtime it changes the layer name from default to "weapon" for no reason.
The layer name "weapon" is what I made for gun objects but other objects seem to change the layer name to "weapon" instead of setting up as "default". Any fixes to this?
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class WeaponManager : NetworkBehaviour {
[SerializeField]
private string weaponLayerName = "Weapon";
[SerializeField]
private Transform weaponHolder;
[SerializeField]
private PlayerWeapon primaryWeapon;
private PlayerWeapon currentWeapon;
private WeaponGraphics currentGraphics;
public bool isReloading = false;
void Start ()
{
EquipWeapon(primaryWeapon);
weaponLayerName = "Default";
}
public PlayerWeapon GetCurrentWeapon ()
{
return currentWeapon;
}
public WeaponGraphics GetCurrentGraphics()
{
return currentGraphics;
}
void EquipWeapon (PlayerWeapon _weapon)
{
currentWeapon = _weapon;
GameObject _weaponIns = (GameObject)Instantiate(_weapon.graphics, weaponHolder.position, weaponHolder.rotation);
_weaponIns.transform.SetParent(weaponHolder);
currentGraphics = _weaponIns.GetComponent<WeaponGraphics>();
if (currentGraphics == null)
Debug.LogError("No WeaponGraphics component on the weapon object: " + _weaponIns.name);
if (isLocalPlayer)
Util.SetLayerRecursively(_weaponIns, LayerMask.NameToLayer(weaponLayerName));
}
Answer by Rygaran · Mar 10, 2018 at 04:52 AM
You're calling EquipWeapon(), wich sets the layer to weaponLayerName,wich is "Weapon", since that's the default, and then it gets set to "Default".