Assign ScriptableObject (Inventory DataBase)
hi i am creating inventory for my game and i got stuck when script need to choose which scriptableObject (DataBase) to use for a different weapon. i want script to look what weapon is active then assign database of that weapon to the script and let script do its magic. I tried setting what dataBase to use with a Resource.load
for (int i = 0; i < Aim.transform.childCount; i++)
{
if (Aim.transform.GetChild(i).gameObject.activeSelf == true)
{
scriptableObject = Resources.Load<ItemWeapon>(Aim.transform.GetChild(i).name);
if (delayActive != true)
{
canAttack = true;
}
break;
}
else
{
canAttack = false;
}
}
i tried cloning the scriptableObject i need with the instatiate and worked to some degree (half of the code didnt work but it could instatiate bullet with force i set but bullet was created at wrong location, that was only thing that worked)
scriptableObject:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New Weapon", menuName = "Weapon")]
public class ItemWeapon : ScriptableObject
{
[Header("Basic Data")]
public float id;
public new string name;
[Multiline]public string description;
public Sprite icon;
[Header("Weapon oriented data")]
[Range(0, 100f)]public float damage;
public GameObject weapon;
[Header("Gun")]
public bool hasBullet;
public bool holdToShot;
public GameObject Bullet;
public GameObject BulletAim;
[Range(1,200f)]public float bulletSpeed;
[Range(0.1f,10)]public float DestroyAfter;
[Range(0,2f)]public float speedOfShooting;
[Header("Sword")]
public float SpeedOfAttack;
public void Shot(Rigidbody2D RB)
{
RB.velocity = Bullet.transform.right * bulletSpeed;
Destroy(Bullet, DestroyAfter);
}
}
and some example of the dataBase being used:
void Attack()
{
if(canAttack == true)
{
scriptableObject.weapon.GetComponent<Animator>().SetBool("MouseClick", true); //activate animation
switch (scriptableObject.hasBullet)
{
case true:
InstantiateBullet(scriptableObject.Bullet);//spawn bullet
SetInputDelay(scriptableObject.speedOfShooting); //setInputDelay (you cant shoot for some time)
break;
case false:
//attack with sword
break;
}
}
else
{
Debuging("CanAttack is false");
}
}
and i always get error :
NullReferenceException: Object reference not set to an instance of an object
UnityEditor.Graphs.Edge.WakeUp () (at <b82d8d0a349d4d70807c2fc5746a710f>:0)
UnityEditor.Graphs.Graph.DoWakeUpEdges (System.Collections.Generic.List`1[T] inEdges, System.Collections.Generic.List`1[T] ok, System.Collections.Generic.List`1[T] error, System.Boolean inEdgesUsedToBeValid) (at <b82d8d0a349d4d70807c2fc5746a710f>:0)
UnityEditor.Graphs.Graph.WakeUpEdges (System.Boolean clearSlotEdges) (at <b82d8d0a349d4d70807c2fc5746a710f>:0)
UnityEditor.Graphs.Graph.WakeUp (System.Boolean force) (at <b82d8d0a349d4d70807c2fc5746a710f>:0)
UnityEditor.Graphs.Graph.WakeUp () (at <b82d8d0a349d4d70807c2fc5746a710f>:0)
UnityEditor.Graphs.Graph.OnEnable () (at <b82d8d0a349d4d70807c2fc5746a710f>:0)
if someone have experience of creating a dataBase i would like to hear how else can i make this work or help me fix this problem
Thanks