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?
you need to tell us why you think newEnemyPrefab is not being "send" if you pass the gameobject as an argument the script will receive it so your issue is somewhere else
That is what I am struggling with. In the inspector for the BattleSystem script I have a public GameObject variable for enemyPrefab. This should be left empty to 'receive' the newEnemyPrefab, correct?
I guess my question is if there is anything specific in relation to calling a prefab as a GameObject that I should be aware of? i see many discussions calling for the use of Resource.Load but not sure if this is applicable in my case?
is maybe "enemyPrefab" an in scene GameObject? or is a prefab from the Assets folder?
Answer by PeckyUK · Dec 02, 2020 at 03:59 PM
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:
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?
Answer by xpcrepair23 · Dec 02, 2020 at 04:10 PM
You tried adding both of the scripts to the prefab?
Your answer
Follow this Question
Related Questions
How to sync "cooldown timer" across multiple prefabs 0 Answers
prefabs break movement scripts, 0 Answers
Firing projectile from Transform. What am I doing wrong? 1 Answer
prefab script accesses prefab values instead of instantiated values - but only in the player 0 Answers
Adding prefab objects to a list on another script and object? 2 Answers