Search Unity

can't seem to get data from scriptableObject

Discussion in 'Scripting' started by twn9009, Jul 11, 2019.

  1. twn9009

    twn9009

    Joined:
    May 2, 2019
    Posts:
    37
    hi im working on a simple inventory system, i have a weapons scriptable object that contains my data, i cant seem to access the equipWeapon variable from my inventory to my player combat script, on play the inventory shows the equipped weapon in the inspector but the player combat script cant get it for some reason and throws an error and breaks everything, probably just missing something simple, apologies in advance for the terrible coding


    scriptableobject
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. [CreateAssetMenu]
    5. public class Weapon : ScriptableObject
    6. {
    7.    
    8.       public WeaponType WeaponType;
    9.     public int heavyAttackDamage;
    10.     public int lightAttackDamage;
    11.     public int heavyKnockbackAmount;
    12.     public int lightKnockbackAmount;
    13.     public int durability;
    14.     public bool unBreakable;
    15.     public float lightAttackCooldown;
    16.     public float heavyAttackCooldown;
    17.     public WeaponSubType weaponSubType;
    18.     public int maxClip;
    19.     public float reloadTime;
    20.     [Range(0,9999)]
    21.     public int currentAmmo;
    22.      
    23. }
    24. public enum WeaponType
    25. {
    26.     melee,
    27.     ranged
    28. }
    29. public enum WeaponSubType
    30. {
    31.     Dagger,
    32.     Sword,
    33.     Shotgun,
    34.     Pistol
    35.  
    36. }
    37.  
    player combat controller - line 50 is where i try get the variable

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerAttack : MonoBehaviour
    7. {
    8.     Animator anim;
    9.     public float lightAttackCooldown;
    10.     public float heavyAttackCooldown;
    11.     public int lightAttackDamage;
    12.     public int heavyAttackDamage;
    13.     bool onCooldown;
    14.     bool heavyAttacked;
    15.     bool lightAttacked;
    16.     public float lightAttackKnockback;
    17.     public float heavyAttackKnockback;
    18.     public Ammo ammo;
    19.     public Inventory inv;
    20.     public Transform firePoint;
    21.     public Transform ejectPoint;
    22.     public GameObject spentShell;
    23.     public GameObject weaponHand;
    24.     public Collider2D weaponCol;
    25.     public Weapon equipWeapon;
    26.     public Animator weaponAnimator;
    27.     public bool advancedAim;
    28.     public GameObject reticle;
    29.     public GameObject slugPrefab;
    30.     public GameObject shotgunPellet;
    31.     void Start()
    32.     {
    33.         anim = GetComponent<Animator>();
    34.         ammo = GetComponent<Ammo>();
    35.         inv.GetComponent<Inventory>();
    36.  
    37.     }
    38.  
    39.  
    40.     void Update()
    41.     {
    42.         if (advancedAim)
    43.         {
    44.            reticle.SetActive(true);
    45.         }
    46.         else if(FindObjectOfType<Aim>())
    47.         {
    48.             reticle.SetActive(false);
    49.         }
    50.         equipWeapon = inv.equipWeapon;
    51.         weaponAnimator = weaponHand.GetComponentInChildren<Animator>();
    52.         weaponCol = weaponHand.GetComponentInChildren<Collider2D>();
    53.         if (equipWeapon.WeaponType == WeaponType.ranged)
    54.         {
    55.             ejectPoint = FindObjectOfType<EjectPoint>().gameObject.transform;
    56.             firePoint = FindObjectOfType<FirePoint>().gameObject.transform;
    57.         }
    58.  
    59.         if (equipWeapon != null)
    60.         {
    61.             lightAttackCooldown = equipWeapon.lightAttackCooldown;
    62.             heavyAttackCooldown = equipWeapon.heavyAttackCooldown;
    63.             lightAttackDamage = equipWeapon.lightAttackDamage;
    64.             heavyAttackDamage = equipWeapon.heavyAttackDamage;
    65.             lightAttackKnockback = equipWeapon.lightKnockbackAmount;
    66.             heavyAttackKnockback = equipWeapon.heavyKnockbackAmount;
    67.         }
    68.         if (equipWeapon.WeaponType == WeaponType.melee && equipWeapon != null)
    69.         {
    70.             anim.SetBool("MeleeWeapon", true);
    71.             anim.SetBool("RangedWeapon", false);
    72.  
    73.             if (Input.GetButtonDown("Fire1") & !onCooldown)
    74.             {
    75.                 onCooldown = true;
    76.                 anim.SetTrigger("MeleeAttackLight");
    77.                 lightAttacked = true;
    78.                 heavyAttacked = false;
    79.                 StartCoroutine(AttackCooldownTimer(lightAttackCooldown));
    80.  
    81.             }
    82.  
    83.             if (Input.GetButtonDown("Fire2") & !onCooldown)
    84.             {
    85.                 onCooldown = true;
    86.                 anim.SetTrigger("MeleeAttackHeavy");
    87.                 heavyAttacked = true;
    88.                 lightAttacked = false;
    89.                 StartCoroutine(AttackCooldownTimer(heavyAttackCooldown));
    90.  
    91.             }
    92.         }
    93.         if (equipWeapon.WeaponType == WeaponType.ranged)
    94.         {
    95.             anim.SetBool("MeleeWeapon", false);
    96.             anim.SetBool("RangedWeapon", true);
    97.             if (Input.GetButtonDown("Fire1") && !onCooldown && ammo.currentAmmoAmount > 0 && !ammo.reloading)
    98.             {
    99.              
    100.                 ammo.currentAmmoAmount--;
    101.                 LightShot();
    102.             }
    103.             if (Input.GetButtonDown("Fire2") && !onCooldown && ammo.currentAmmoAmount > 0 && !ammo.reloading)
    104.             {
    105.              
    106.                 ammo.currentAmmoAmount--;
    107.                 HeavyShot();
    108.             }
    109.         }
    110.     }
    111.  
    112.     public void LightShot()
    113.     {
    114.         anim.SetTrigger("Shoot");
    115.         Instantiate(shotgunPellet, firePoint.position + new Vector3(0, UnityEngine.Random.Range(0.01f, 0.6f)), firePoint.rotation);
    116.         Instantiate(shotgunPellet, firePoint.position, firePoint.rotation);
    117.         Instantiate(shotgunPellet, firePoint.position - new Vector3(0, UnityEngine.Random.Range(0.01f, 0.6f)), firePoint.rotation);
    118.         weaponAnimator.SetTrigger("Shoot");
    119.         onCooldown = true;
    120.         StartCoroutine(AttackCooldownTimer(lightAttackCooldown));
    121.  
    122.         if (equipWeapon.weaponSubType == WeaponSubType.Shotgun)
    123.         {
    124.             Instantiate(spentShell, ejectPoint.position, ejectPoint.rotation);
    125.         }
    126.     }
    127.     public void HeavyShot()
    128.     {
    129.         anim.SetTrigger("Shoot");
    130.         Instantiate(slugPrefab, firePoint.position, firePoint.rotation);
    131.         weaponAnimator.SetTrigger("Shoot");
    132.         onCooldown = true;
    133.         StartCoroutine(AttackCooldownTimer(heavyAttackCooldown));
    134.  
    135.         if (equipWeapon.weaponSubType == WeaponSubType.Shotgun)
    136.         {
    137.             Instantiate(spentShell, ejectPoint.position, ejectPoint.rotation);
    138.         }
    139.  
    140.     }
    141.  
    142.     IEnumerator AttackCooldownTimer(float attackCooldown)
    143.     {
    144.  
    145.  
    146.         yield return new WaitForSeconds(attackCooldown);
    147.         onCooldown = false;
    148.         heavyAttacked = false;
    149.         lightAttacked = false;
    150.  
    151.  
    152.     }
    153.  
    154.  
    155.     private void OnTriggerEnter2D(Collider2D collision)
    156.     {
    157.         if (collision.GetComponent<Collider2D>().tag == "Enemy")
    158.         {
    159.             if (lightAttacked)
    160.             {
    161.                 collision.GetComponent<EnemyHealth>().hp = collision.GetComponent<EnemyHealth>().hp - lightAttackDamage;
    162.                 collision.GetComponent<Rigidbody2D>().AddForce(new Vector2(lightAttackKnockback, 0));
    163.  
    164.                 collision.GetComponent<SpriteRenderer>().color = Color.red;
    165.                 StartCoroutine(HitTimer());
    166.  
    167.                 IEnumerator HitTimer()
    168.                 {
    169.  
    170.  
    171.                     yield return new WaitForSeconds(0.1f);
    172.  
    173.                     collision.GetComponent<SpriteRenderer>().color = Color.white;
    174.  
    175.                 }
    176.  
    177.  
    178.  
    179.  
    180.             }
    181.             if (heavyAttacked)
    182.             {
    183.                 collision.GetComponent<EnemyHealth>().hp = collision.GetComponent<EnemyHealth>().hp - heavyAttackDamage;
    184.                 collision.GetComponent<Rigidbody2D>().AddForce(new Vector2(lightAttackKnockback, 0));
    185.                 collision.GetComponent<SpriteRenderer>().color = Color.red;
    186.                 StartCoroutine(HitTimer());
    187.  
    188.                 IEnumerator HitTimer()
    189.                 {
    190.  
    191.  
    192.                     yield return new WaitForSeconds(0.1f);
    193.  
    194.                     collision.GetComponent<SpriteRenderer>().color = Color.white;
    195.  
    196.                 }
    197.  
    198.             }
    199.  
    200.         }
    201.     }
    202. }
    203.  
    inventory

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Inventory : MonoBehaviour
    6. {
    7.    public List<Weapon> allWeapons;
    8.     public List<GameObject> allWeaponGO;
    9.    public List<Weapon> inInv;
    10.    public List<Weapon> startItems;
    11.     public Weapon equipWeapon;
    12.  
    13.   public  int currentWeapon;
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         SetStartItems();
    18.         allWeaponGO[0].SetActive(true);
    19.         equipWeapon = inInv[0];
    20.         currentWeapon = 0;
    21.     }
    22.  
    23.      
    24.  
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.         currentWeapon = Mathf.Clamp(currentWeapon, 0, inInv.Count -1);
    30.         WeaponSwap();
    31.         Debug.Log(equipWeapon.name);
    32.     }
    33.     void SetStartItems()
    34.     {
    35.         foreach(Weapon weapon in startItems)
    36.         {
    37.             inInv.Add(weapon);
    38.         }
    39.     }
    40.     void WeaponSwap()
    41.     {
    42.      
    43.      
    44.     }
    45. }
    46.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    3,776
    Your best bet for tackling this is going to be to put in Debug.Log() statements listing the name of the item in equipWeapon, etc. at strategic points, such as right before it gives you the error.

    Is it possible you dragged this onto more than one GameObject and only set it up on one object? Right before the offending line, do a Debug.Log(name) to see the name of the GameObject it is on.
     
  3. twn9009

    twn9009

    Joined:
    May 2, 2019
    Posts:
    37
    the setup for the gameObjects looks fine, the player combat and inventory scripts are only on the player, so getcomponent<Inventory>()should work fine, the inventory script works it shows the equipped weapon, the player combat script just cant access it, gonna play around with execution order of my scripts and see if it fixes it, debug.log(name) isnt working because it has a null reference and will just spit out errors instead of logging the gameobject name, i just did a check for references in scene and there is defo only one script in the scene
     
  4. twn9009

    twn9009

    Joined:
    May 2, 2019
    Posts:
    37
    okay that's very weird, changed equipWeapon = inv.equipWeapon; to equipWeapon = GetComponent<Inventory>().equipWeapon; and now it works even though i was getting the component in start anyway, but whatever i guess, if anyone can tell me why this happened so i could learn from it i'd be very grateful
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    3,776
    Line 35 in PlayerAttack does nothing. It just gets the component and does nothing with it.

    Generally you want to follow some kind of convention to help identify which fields are fields YOU will fill out by doing your GetComponent<>() calls, and which scripts should be filled out in the inspector.

    For editor fill-in, it should be:

    Code (csharp):
    1. public ThingType MyThing;
    For code load-up, it should be:

    Code (csharp):
    1. private ThingType myThing;
    or some such convention. Some people insist on EVERYTHING being private and then decorating it with a SerializeFieldAttribute:

    Code (csharp):
    1. [SerializeField] private ThingType MyThing;
    But I find this gross visually, but your mileage may vary.
     
  6. twn9009

    twn9009

    Joined:
    May 2, 2019
    Posts:
    37
    just spotted it thanks was meant to be an = not a . so that inv = getcomponent<Inventory>() doesn't matter now because i changed the script around and it works but thanks for spotting that it was going to drive me crazy
     
    Kurt-Dekker likes this.