Accessing variables inside a scriptable object.

I am still having trouble getting a script to access the variables inside a scriptable object. I was wondering whether anyone could teach me how to do this, please?

I have a scriptable object that contains :

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    [CreateAssetMenu(fileName = "Enemy", menuName = "Enemy")]
    
    public class EnemyTemplate : ScriptableObject
    {
    
        public string monsterName;
    
    }

And I have created one of these enemies and called it ‘Enemy’, with the monsterName set to ‘Sea Monster’.

Then I have one script which contains :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyEncounter : MonoBehaviour
{

    public BattleManager battleManager;

    public Enemy encounteredEnemy;

    void OnTriggerEnter2D(Collider2D enter)
    {

        battleManager.enemy = encounteredEnemy;

        battleManager.BattleStart();

    }

}

Where into the inspector slot for encountered enemy, I place ‘Enemy’. There is also another script, called the BattleManager, which contains :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BattleManager : MonoBehaviour
{

    public Text enemyName;

    public Enemy enemy;

    public void BattleStart()
    {

        enemyName.text = "";

        enemyName.text = enemy.monsterName;

    }

}

However, this last script doesn’t seem to work as “enemy.monsterName” gets a red line underneath it, apparently because it can’t implicitly convert a scriptable object into a string.

I would be very grateful if someone could teach me how to set the text ‘enemyName’ to whatever the ‘monsterName’ in the scriptable objects that it receives is set to?

Thank You.

Your variable enemy is created as type Enemy:

 public Enemy enemy;

are you sure that this is correct? Is there a class named Enemy somewhere?

From what you describe it should be:

 public EnemyTemplate enemy;

Then you can assign the Scriptable object class which is named EnemyTemplate and actually contains that public monsterName field that you describe.

Aha! It worked! Thank you so much! I tried that before but must have done something wrong! XD Awesome, thanks!