Loading prefab 'sent' from other script?

Hello,

I have a two scripts which i have connected. One is a battlesystem and the other is a script that sends information about an enemy to that battlesystem (the game has an overworld with a separate battle screen for turn based combat). The things that I want it to send are the enemy attributes, move list and a prefab which contains the enemy sprite and animations. At the moment the scripts work fine in transferring the attributes and move lists but not the prefab. I’ve had a look around and there seems to be a lot of questions and solutions regarding loading prefabs but I’m not sure if they fit with what I am trying to do, or whether what I am trying to do is the right thing.

Here is the ‘Send to Battle’ script:

public class EnemyBattleTransition : MonoBehaviour
{
public Attributes enemyAttributes;
public EnemyMoveList enemyMoveList;
public GameObject enemyPrefab;
public BattleSystem battleSystem;
public string sceneToLoad = “Battle Screen”;

public void SendToBattle()
{
    //send info to battlesystem
    battleSystem.SetupEnemy(enemyAttributes, enemyMoveList, enemyPrefab);
    SceneManager.LoadScene(sceneToLoad);
}

and here is the part of the battle system which relates to it:

void Start()
{
    GameObject player = Instantiate(playerPrefab, playerBattlestation) as GameObject;
    GameObject enemy = Instantiate(enemyPrefab, enemyBattlestation) as GameObject;
    playerAnimator = player.GetComponent<Animator>();
    enemyAnimator = enemy.GetComponent<Animator>();
    state = BattleState.START;
    StartCoroutine(SetupBattle());
}

public void SetupEnemy(Attributes newEnemyAttributes, EnemyMoveList newEnemyMoveList, GameObject newEnemyPrefab)
{
    enemyAttributes = newEnemyAttributes;
    thisEnemyMoveList = newEnemyMoveList;
    enemyPrefab = newEnemyPrefab;
}

So the SetupEnemy function is working to bring in attributes and enemymovelist but not the prefab.

Help?

I thought I had been able to resolve this by adding a ‘receiver’ into my battlescreen which receives the variables sent by the script within the overworld (other scene). This works in unity however when I build i get a null reference exception since the BattleSystem script is unable to instantiate the enemyPrefab.

My guess is that is has something to do with the method I am using to send the variables over. This is the code for sending the enemy variables to the battle:

public class EnemyBattleTransition : MonoBehaviour
{
    public Attributes enemyAttributes;
    public EnemyMoveList enemyMoveList;
    public GameObject enemyPrefab;
    public EnemyBattleReciever reciever;
    public string sceneToLoad = "Battle Screen";

    public void SendToBattle()
    {
        reciever.SetupEnemy(enemyAttributes, enemyMoveList, enemyPrefab);
        SceneManager.LoadScene(sceneToLoad);
    }
}

Within here I have to save the ‘receiver’ as a prefab and drag it into the inspector for the EnemyBattleTransition script. Perhaps this is causing a break when the game is built?
Here is what it looks like in the inspector:
171936-bph-bs1.png

Here is the code for the receiver:

public class EnemyBattleReciever : MonoBehaviour
{
    public Attributes enemyAttributes;
    public EnemyMoveList thisEnemyMoveList;
    public GameObject enemyPrefab;

    // Start is called before the first frame update
    void Start()
    {
        SetupEnemy(enemyAttributes, thisEnemyMoveList, enemyPrefab);
    }

    public void SetupEnemy(Attributes newEnemyAttributes, EnemyMoveList newEnemyMoveList, GameObject newEnemyPrefab)
    {
        enemyAttributes = newEnemyAttributes;
        thisEnemyMoveList = newEnemyMoveList;
        enemyPrefab = newEnemyPrefab;
    }
}

It will always be the same single receiver in the battle screen. Should I code a hard connection to this?

You tried adding both of the scripts to the prefab?