Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Storing Sub items in an item class?

Discussion in 'Scripting' started by carringtongames201998, Jan 2, 2020.

  1. carringtongames201998

    carringtongames201998

    Joined:
    Dec 28, 2019
    Posts:
    7
    I am trying to make a weapon system for my game where a Guns (which are considered items) can hold other items on them (such as attachments). Is this possible using the same Item class?
     
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    488
    I mean I don't see why it wouldn't be possible.
     
  3. KubesContent

    KubesContent

    Joined:
    Jul 13, 2018
    Posts:
    10
    This should be possible.

    This is an example of what you may be looking for:

    Code (CSharp):
    1. public abstract class Item : ScriptableObject
    2.  
    3.  
    4. {
    5.     [SerializeField]
    6.     private string name;
    7.  
    8.     [SerializeField]
    9.     private int value;
    10.  
    11.     public string Name => name;
    12.  
    13.     public int Value => value;
    14. }
    15.  
    16.  
    17. public class Weapon : Item
    18. {
    19.  
    20.     [SerializeField]
    21.     private int basePower;
    22.  
    23.     [SerializeField]
    24.     private WeaponAttachment[] attachments = new WeaponAttachment[4];
    25.  
    26.  
    27.     public int Power
    28.     {
    29.         int totalPower = basePower;
    30.        
    31.         foreach(var attachment in attachments.Where(attachment => attachment))
    32.         {
    33.             totalPower += attachment.PowerBoost;
    34.         }
    35.  
    36.         return totalPower;
    37.     }
    38.  
    39.     public void Attach(WeaponAttachment newAttachment)
    40.     {
    41.         for (int i = 0; i < attachments.Length; i++)
    42.         {
    43.             if (attachments[i]) // true if attachment is not null
    44.             {
    45.                 continue;
    46.             }
    47.  
    48.             attachments[i] = newAttachment;
    49.             return;
    50.         }
    51.  
    52.         throw new OutOfRangeException();
    53.     }
    54.  
    55. }
    56.  
    57.  
    58. public class WeaponAttachment : Item
    59. {
    60.     [SerializeField]
    61.     private int powerBoost;
    62.  
    63.     public int PowerBoost => powerBoost;
    64. }
     
  4. carringtongames201998

    carringtongames201998

    Joined:
    Dec 28, 2019
    Posts:
    7
    Ok, but when I place this script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ItemDatabase : MonoBehaviour
    6. {
    7.     public List<Item> gameItems = new List<Item>();
    8. }
    9.  
    I cannot set the values of the items in the list manually. How to fix?
     
  5. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181
  6. KubesContent

    KubesContent

    Joined:
    Jul 13, 2018
    Posts:
    10
    You mean you can not set them in the inspector?

    As long as the size of the list is not zero and you are trying to drag and drop objects that are an item or a GameObject with an Item component (if it inherits MonoBehaviour), you should be able to add that object to the list.