I am setting up a turn based battle system where the player is in a platforming world and on collision will enter a traditional turn based battle scene. I have the scene loading correctly on collision. My current step is loading in the correct enemies based on which enemy the player collides with in the platforming world. Each enemy has an EnemiesToBattle script that I attach an enemy prefab to. However, I’m having trouble with my script ‘‘telling’’ how many and which enemies to load into the battle scene.
Below shows on collision I am calling LoadEnemies() (full function shown in the second screenshot), which takes in an EnemyStats[ ] array, of which every enemy prefab has an EnemyStats script attached to.
I am trying to have my GetEnemies() function basically say “If there is one enemy to load attached to this enemy, load that enemy into the battle scene, if there’s 2, load 2, etc.” but my GetEnemies() function is saying not all code paths return a value.
When I manually code in something along the lines of:
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime, such as values related to the terminating condition for your for() loop.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run?
what are the values of the variables involved? Are they initialized?
Knowing this information will help you reason about the behavior you are seeing.
You could also just display various important quantities in UI Text elements to watch them change as you playtest.
If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:
Okay. I’ve reworked my code to Debug out what is actually being loaded into the array and the correct game objects are being loaded in. I also have set the variable that is the array to be returned through my function, which then SHOULD be sent to the LoadEnemies() function, but it my GetEnemy() function is saying "Cannot implicitly convert from type EnemyStats[ ] to EnemyStats. Code below:
private void OnCollisionEnter2D(Collision2D other)
{
if(other.gameObject.tag == "Player")
{
SceneManager.LoadScene("BattleScene");
EnemyLoader.instance.LoadEnemies(new EnemyStats[] { GetEnemies() }) ;
}
}
public EnemyStats[] GetEnemies()
{
EnemyStats[] enemiesToLoad = new EnemyStats[EnemiesToBattle.instance.enemy.Length];
for(int i = 0; i < GetComponent<EnemiesToBattle>().enemy.Length; i++)
{
enemiesToLoad[i] = EnemiesToBattle.instance.enemy[i];
Debug.Log("loaded enemy " + enemiesToLoad[i]);
}
Debug.Log("enemy at pos1 " + enemiesToLoad[0] + "enemy at pos2 " + enemiesToLoad[1]);
return enemiesToLoad;
I’ve tried researching fixes for something like this as well as following my code through to the end, but everything I can find says it is of type EnemyStats[ ]. Any help is appreciated.