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

Question Making Database Script Objects Displayed and Selectable in the Inspector

Discussion in 'Scripting' started by salehi-gamedesign, May 6, 2020.

  1. salehi-gamedesign

    salehi-gamedesign

    Joined:
    Apr 30, 2018
    Posts:
    15
    I have a database script, SpellDatabase.cs which creates a number of spell objects in void Start() using the class script, Spell.cs. Each object contains various properties such as projectile force, damage, lifetime etc.

    I have created projectile prefabs in Unity for each of the spells contained in the database. Each prefab has the script, 'Projectile.cs' attached to them. Using the Unity Inspector I want to be able to select the Spell object from SpellDatabase.cs which contains the corresponding spell properties for the projectile prefab I am working on. How do I reference SpellDatabase.cs in the Projectile.cs script so that I can achieve this?

    Thanks.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Use a singleton pattern for your SpellDatabase component. Here's a simple example: https://gamedev.stackexchange.com/a/116010

    Using that code you can call "SpellDatabase.Instance" from your projectile class to get your database instance.
     
  3. salehi-gamedesign

    salehi-gamedesign

    Joined:
    Apr 30, 2018
    Posts:
    15
    Thanks for your reply. I've added a singleton pattern to my SpellDatabase script however I'm unclear how SpellDatabase.Instance can be used to allow me to select specific SpellDatabase objects from the Inspector.

    My spell projectile prefabs have a Projectile.cs script and a field named 'spellProperties'. I want to be able to assign the relevant SpellDatabase object pertaining to the spell prefab I'm working on to 'spellProperties' in the Inspector, e.g. when building the fire spell projectile prefab I want to be able to select the SpellDatabase object which contains the fire spell properties (SpellDatabase.Instance.fire) through the Inspector instead of having it hardcoded into unique projectile scripts for each spell prefab.

    Apologies for any confusion.
     
  4. salehi-gamedesign

    salehi-gamedesign

    Joined:
    Apr 30, 2018
    Posts:
    15
    My initial post may have been difficult to follow so I'll word it differently: How can I make the instances derived from my Spell class and created in my SpellDatabase script (e.g. Spell.fire, Spell.ice, Spell.water etc.) be displayed in a selection-styled drop-down field in the the Unity Inspector?

    Hopefully that better communicates my aim. Thanks in advance.

    Edit: Corrected instance notation errors.