- Home /
Function not excuting dispite being called
Sorry to pester you all again, but I have another scripting problem. For context, I'm working on a turn-based RPG battle system, I'm having fun programming it but something really weird is happening (here's my code for reference).
///<summary>The folowing code outlines the functions and routines that make up the battle system,
///to begin with the program fetches the stats of the enemy and the player (HP, MP, Strength
///and resislience) and makes them the value of their respective varibles.
///The main loop can then begin, the progam will first check the HP values of both parties
///player first, to determine wether their has been a win or a loss.
/// A "coin" is then tossed to decide turn order, if the coin "lands" on "heads"
/// the player can take their action first on that turn. </summary>
//Libaries are called here.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Battle_System : MonoBehaviour
{
//Defintion of public varibles
public Text BattleLog; //The battle log, tells the player what's happening.
public Text HPCount; //The HP count, shows the remaining HP of the player.
public Button Slash; //The button for the slash action, dose a physical attack
//Player Stats
public float P_HP;
public float P_MP;
public float P_Strength;
public float P_Resilience;
//Enemy Stats
public float E_HP;
public float E_MP;
public float E_Strength;
public float E_Resilience;
//Definiton of private varibles
private int coin; //The coin that'll be tossed before each round
private float damage; //How much damage a target takes from an attack, used in calculations
//Used in making the pauses between battle log changes
float _interval = 1.5f;
float _lastTime;
//Initiates the varibles
void Start()
{
//Explains to the player that they've encountered an enemy.
BattleLog.text = ("Enemy approaches...");
//Gives the player's stats a value
P_HP = 20;
P_MP = 10;
P_Strength = 5;
P_Resilience = 2;
//Gives the Enemy's stats a value
E_HP = 12;
E_MP = 10;
E_Strength = 5;
E_Resilience = 2;
// Tells the player their current HP
HPCount.text = ("HP: " + P_HP);
//Used in making the pauses between battle log changes, credit to kubajs for their
//help with this
_lastTime = Time.time;
//Sets the existance of the Action buttons (Slash, Cast, Examine, and Satchel)
//to false to stop the player spamming it and messing up the game
}
//The Main Loop
void Update()
{
// The program waits 5 seconds to allow the player to read the battle log
if (Time.time > _lastTime + _interval)
{
_lastTime = Time.time;
//The HP of both parties are checked to dermine whether the battle has been won or lost
if (P_HP <= 0)
{
//Player has no HP, player loses
BattleLog.text = "You Lose...";
}
else if (E_HP <= 0)
{
BattleLog.text = "You Win!";
}
//Only goes through the following if both sides have HP remaining
else
{
//The coin is flipped to detrmine the turn order for this round.
coin = Random.Range(0, 2);
Debug.Log("Coin=" + coin);
if (coin == 0)
{
//Heads, player goes first
BattleLog.text = "Your turn";
}
else if (coin == 1)
{
//Tails, enemy goes first
BattleLog.text = "Enemy's turn";
EAttack();
}
}
}
}
//Defintion of functions
//Function for when the enemy makes a physical attack
void EAttack()
{
//Game pauses to allow the player to read battle log
if (Time.time > _lastTime + _interval)
{
_lastTime = Time.time;
BattleLog.text = "Enemy attacks!";
if (Time.time > _lastTime + _interval)
{
_lastTime = Time.time;
//Another pause is done before damage calculation
damage = (E_Strength * Random.Range(0.8f, 1.2f)) - (P_Resilience * Random.Range(0.8f, 1.2f));
Debug.Log("Damage taken before rounding= " + damage);
//Rounds the damage down to the nearest whole number
damage = Mathf.Floor(damage);
Debug.Log("Damage taken after rounding= " + damage);
//If the damage taken is less than 1, the amount of damage taken becomes 1.
if (damage < 1)
{
damage = 1;
}
//The amount of damage taken is recorded in the battle log.
BattleLog.text = "You suffered " + damage + " points of damage.";
//Damage is applied to player HP
P_HP = P_HP - damage;
//Updates the HP count.
HPCount.text = ("HP: " + P_HP);
}
}
}
}
If you've managed to follow my comments so far across this monstrosity you'd notice a function called "EAttack()", when called this is supposed to make the enemy attack the player. But for some bizarre reason when unity gets to the line of code denoting to execute that function it completely skips over it like it didn't exist. Did I do something wrong?
Your answer
Follow this Question
Related Questions
I have a bug with my UI. 1 Answer
Outline Shader not working correctly on blender models 0 Answers
Sprite shrinks when selected? 1 Answer
Move GameObject between two points using Scrollbar 2 Answers
Double Score Problem ? 1 Answer