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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Access ScriptableObject information.

Discussion in 'Scripting' started by JeaNiX, Aug 15, 2021.

  1. JeaNiX

    JeaNiX

    Joined:
    Oct 31, 2016
    Posts:
    20
    Hello,
    currently I'm trying to access the information I'm storing in a Scriptable Object. I have a GameManager Script in which I try to make a character selection menu, but I want that while switching the character the description and the name will both come from the Scriptable Object of that character. Also, all my character prefabs have a Character script, which inherits from "Hero" Script, which in my case controls the process of updating information.
    Problem is, even while debugging I get and empty text. What is the best way to access the .Name in the Hero.cs.
    Code (CSharp):
    1. public class Hero : MonoBehaviour
    2. {
    3.     [SerializeField] private protected HeroSObject sObject;
    4.  
    5.  
    6.     private string _name;
    7.  
    8.  
    9.  
    10.     public string Name
    11.     {
    12.         get
    13.         {
    14.             return _name;
    15.         }
    16.     }
    17.     void Start()
    18.     {
    19.  
    20.     }
    21.     void Update()
    22.     {
    23.      
    24.     }
    25.     private protected void SetUpBaseData()
    26.     {
    27.         _name = sObject.Name;
    28.     }
    Code (CSharp):
    1. public class HeroSObject : ScriptableObject
    2. {
    3.     [SerializeField] private string _name;
    4.  
    5.  
    6.  
    7.     public string Name
    8.     {
    9.         get
    10.         {
    11.             return _name;
    12.         }
    13.     }
    14. }
    After in my Wiz script I call the SetUpBaseData() and it adds the info about the name of the character to the game Object.
    And this is how I try to access it:
    Code (CSharp):
    1.         heroName.text = heroes[_currentHero].GetComponent<Hero>().Name;
    Where heroes[] is just an array of hero prefabs. They spawn good, but I can't access and so print the text.
    Thanks.
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    If you ran SetUpBaseData() on the hero instance and it's still blank name, maybe you're referencing the prefab instead of the instance.
     
  3. JeaNiX

    JeaNiX

    Joined:
    Oct 31, 2016
    Posts:
    20
    If I run
    Code (CSharp):
    1.         Debug.Log("Name from SO is : " + Name);
    from Wiz.cs i do get the console output I need, so the instantiated object is referencing the SO.