Скрипт:
using UnityEngine;
public class WeaponSwitcher : MonoBehaviour {
public BaseWeapon cur_weapon;
public BaseWeapon[] collection;
public BaseWeaponSlot _base;
public SecondaryWeaponSlot _pistol;
public string bs_name;
public string scnr_name;
private int cur_slot = 1;
void OnEnable()
{
WeaponPickUp(bs_name);
WeaponPickUp(scnr_name);
}
void WeaponPickUp(string tag)
{
foreach (BaseWeapon wpn in collection)
{
int i = 0;
if (wpn.tag == tag)
{
Debug.Log("Good weapon: " + wpn.tag);
if (wpn is BaseWeaponSlot) { PickUpBaseCheck(); wpn.IsPresent = true; collection[i] = _base; CallIsActive(); }
if (wpn is SecondaryWeaponSlot) { PickUpPistolCheck(); wpn.IsPresent = true; collection[i] = _pistol; CallIsActive(); }
}
else
{
Debug.Log("Bad weapon has detected " + wpn.tag);
wpn.IsPresent = false;
wpn.IsActive();
}
i++;
}
}
void FixedUpdate()
{
if (Input.GetKeyDown(KeyCode.Alpha1)) { cur_slot = 1; }
if (Input.GetKeyDown(KeyCode.Alpha2)) { cur_slot = 2; }
if (cur_slot == 1) { _base.IsPresent = true; _pistol.IsPresent = false; CallIsActive(); cur_weapon = _base; }
if (cur_slot == 2) { _base.IsPresent = false; _pistol.IsPresent = true; CallIsActive(); cur_weapon = _pistol; }
}
void PickUpBaseCheck()
{
if (_base != null) { _base.IsPresent = false; }
}
void PickUpPistolCheck()
{
if (_pistol != null) { _pistol.IsPresent = false; }
}
void CallIsActive()
{
if (_base != null) { _base.IsActive(); }
if (_pistol != null) { _pistol.IsActive(); }
}
}
// В логе говорится NullReferenceException: Object reference not set to an instance of an object
WeaponSwitcher.WeaponPickUp (System.String tag) (at Assets/Game/Weapon/Script/WeaponSwitcher.cs:32)
WeaponSwitcher.OnEnable () (at Assets/Game/Weapon/Script/WeaponSwitcher.cs:21) Нужно что бы скрипт работал при не наличии оружия, но увы, не работает.