Values from another script not assigning to variable
I have 4 monsters that each have a script called EnemyBattle that holds their stats. I have another script called BattleManager that gets their stats and assigns them to another variable on Awake. This was working perfectly fine for awhile but then out of nowhere the values for all but one of the monsters aren't getting assigned to the variables. I fixed this issue the first time by deleting the objects that were giving me the issue and duplicating the single monster that was returning it's stats but now I have the same issue again but with only 2 of them. I don't ever recall doing anything in the inspector to the GameObjects so I have no clue why this is happening and I have to assume it's a bug. I've tried restarting Unity but that still didn't work. Here's the code for reference:
void Awake {
enemy1 = GameObject.Find("Dragon_1");
enemy2 = GameObject.Find("Dragon_2");
enemy3 = GameObject.Find("Dragon_3");
enemy4 = GameObject.Find("Dragon_4");
EnemyBattle eb1 = enemy1.GetComponent<EnemyBattle>();
EnemyBattle eb2 = enemy2.GetComponent<EnemyBattle>();
EnemyBattle eb3 = enemy3.GetComponent<EnemyBattle>();
EnemyBattle eb4 = enemy4.GetComponent<EnemyBattle>();
espeed1 = eb1.GetSpeed();
espeed2 = eb2.GetSpeed();
espeed3 = eb3.GetSpeed();
espeed4 = eb4.GetSpeed();
}
Before it would only get the speed for enemy4 but now it gets the speed for only enemy1 and enemy4.
I also have an Animator connected to each enemy and call their animations just fine.
Here's what it looks like in the inspector
I can probably just delete them and duplicate again but I would like to avoid this problem or at least identify it if I can so can someone please help me?
Does anyone have an answer? I still can't figure out why it does this. It even happens when I build the game and it's very problematic. Should also add that I have another script that gets their speed just fine so I guess something must be wrong with this one?
public class EnemyLaneTrigger : $$anonymous$$onoBehaviour
{
public string name;
public int speed = 0;
public GameObject enemy1 = null;
public GameObject enemy2 = null;
public GameObject enemy3 = null;
public GameObject enemy4 = null;
public int espeed1;
public int espeed2;
public int espeed3;
public int espeed4;
void Start ()
{
enemy1 = GameObject.Find("Dragon_1");
enemy2 = GameObject.Find("Dragon_2");
enemy3 = GameObject.Find("Dragon_3");
enemy4 = GameObject.Find("Dragon_4");
EnemyBattle eb1 = enemy1.GetComponent<EnemyBattle>();
EnemyBattle eb2 = enemy2.GetComponent<EnemyBattle>();
EnemyBattle eb3 = enemy3.GetComponent<EnemyBattle>();
EnemyBattle eb4 = enemy4.GetComponent<EnemyBattle>();
espeed1 = eb1.Speed;
espeed2 = eb2.Speed;
espeed3 = eb3.Speed;
espeed4 = eb4.Speed;
}
public class Battle$$anonymous$$anager : $$anonymous$$onoBehaviour
{
public int eSpeed1;
public int eSpeed2;
public int eSpeed3;
public int eSpeed4;
public GameObject enemy1 = null;
public GameObject enemy2 = null;
public GameObject enemy3 = null;
public GameObject enemy4 = null;
void Start ()
{
enemy1 = GameObject.Find("Dragon_1");
enemy2 = GameObject.Find("Dragon_2");
enemy3 = GameObject.Find("Dragon_3");
enemy4 = GameObject.Find("Dragon_4");
EnemyBattle eb1 = enemy1.GetComponent<EnemyBattle>();
EnemyBattle eb2 = enemy2.GetComponent<EnemyBattle>();
EnemyBattle eb3 = enemy3.GetComponent<EnemyBattle>();
EnemyBattle eb4 = enemy4.GetComponent<EnemyBattle>();
eSpeed1 = eb1.Speed;
eSpeed2 = eb2.Speed;
eSpeed3 = eb3.Speed;
eSpeed4 = eb4.Speed;
}
EnemyLaneTrigger gets the speeds just fine but Battle$$anonymous$$anager won't
Answer by GPSGAME · Jun 30, 2020 at 05:15 PM
@ColonelOmen I am trying to make a game similar like yours. did u get the solution. please share it