Changing ScriptableObject to another during runtime crashes UnityEditor
I found that that this was not the issue however I still do not have a fix. When killing enemies, the editor freezes. My new post.
I have an inventory system that uses scriptableobjects for the items(ItemObject), I have an enemy that on death instantiates a 'lootbag' prefab with the item script on it which references one of the scriptableobject ItemObjects. On my enemy script I have a List<> containing possible ItemObject drops to set that 'lootbag's ItemObject to. Sometimes when they die it functions fine, other times it permanently freezes the editor/makes it go unresponsive. Enemy's Line:
reward_object.GetComponent<Item>().SetItem(rewards[Random.Range(0, rewards.Count - 1)]);
Item's Line:
public void SetItem(ItemObject _itemObject)
{
item = _itemObject;
}
If I need to provide more of the script please let me know. Using Unity version 2020.3.3f1
Anything in the logs?
rewards[Random.Range(0, rewards.Count + 1)]
should be rewards[Random.Range(0, rewards.Count)]
by the way. Random.Range excludes the provided upper bound but if Random.Range
returns rewards.Count
, you'll have an IndexOutOfRangeException
As in console logs or should I try and find a crash log somewhere? If console logs than no there's no error it just instantlly freezes after instantiating the lootbag. Perhaps that's the issue, I was reading into random.range and found that the upper number was excluded so I thought I'd need to increase it. Thank you!
Well, I guess I wasn't thinking through it very well initially since the List's index starts at 0 so I ended up changing this rewards[Random.Range(0, rewards.Count + 1)];
to rewards[Random.Range(0, rewards.Count - 1)];
but sadly the issue remains, sometimes the item will drop correctly but sometimes it'll make the editor go unresponsive permanently.
You need rewards[Random.Range(0, rewards.Count)]
, not rewards[Random.Range(0, rewards.Count - 1)]
But anyway, I really doubt this is the cause of your issue. A hanging editor usually comes from an infinite loop.
Your answer
Follow this Question
Related Questions
Scriptable Objects and Assetbundles 0 Answers
ScriptableObject Referencing 1 Answer
Why can't I set an Animation reference in a Scriptable Object instance? 1 Answer
Scripts vs ScriptableObject 1 Answer
ScriptableObject Life Cycle [ Help ] 0 Answers