I'm writing a game where I'm using scriptable objects for the first time. And I ran into an error. "NullReferenceException: Object reference not set to an instance of an object". I have no idea how to solve this problem, but I really need to do it. I've tried using the Start method, Awake, and even calling a separately created method, which checks to see if the values are set when any method is executed. I need to somehow read the data from the scriptable object in order to enter it into the fields of the class. However, I do not know of any method that could allow me to do this. Tell me what can be used, or how to read the data. enter image description here
using UnityEngine;
using UnityEngine.EventSystems;
public class OnEntityClickHandler : MonoBehaviour, IPointerClickHandler
{
public void OnPointerClick(PointerEventData eventData)
{
Skeleton skeleton = gameObject.GetComponent<Skeleton>();
skeleton.TakeDamage(GetDamage(skeleton.Type));
}
private uint GetDamage(IElement elementType)
{
uint totalDamage = 0;
foreach(GameObject i in PerkController.Perks)
{
Perk perk = i.GetComponent<Perk>();
totalDamage += perk.GetDamage(elementType); // Error
}
return totalDamage;
}
}
using UnityEngine;
public class Perk : MonoBehaviour
{
[SerializeField]
private PerkData _data;
[SerializeField]
private GameObject _upgradeDrop;
private string _name;
private string _description;
private uint _upgradeCost;
private uint _damage;
private IElement _type;
private uint _level = 1;
private float _index = 1.5f;
public string Name => _name;
private void Start()
{
_name = _data.Name;
_description = _data.Description;
_upgradeDrop = _data.UpgradeDrop;
_upgradeCost = _data.UpgradeCost;
_damage = _data.Damage;
_type = ElementController.GetElement(_data.Type);
}
public uint GetDamage(IElement elementType)
{
return (uint)(_type.GetIndex(elementType) * _damage * _level);
}
public bool TryUpgrade()
{
string upgradeDropName = _upgradeDrop.GetComponent<Drop>().Name;
if (DropController.TryDecreaseCount(upgradeDropName, _upgradeCost))
{
_level++;
_upgradeCost = (uint)(_upgradeCost * _index);
return true;
}
else
{
return false;
}
}
}
using UnityEngine;
public class PerkController : MonoBehaviour
{
[SerializeField]
private GameObject[] _perks;
public static GameObject[] Perks;
private void Start()
{
Perks = _perks;
}
}