- Home /
1 of the 2 GameObjects with the same MonoBehavior script somehow ignores an If conditional?
Programming a Turn-Based RPG in which for normal attacks a character can strike for X amount of times depending on their Level.
A CharacterManager script handles that part of the calculation with the following code, which works perfectly.
// Called at Start of CharacterManager only if the Manager has a ScriptableObject with the Character' Data for their Stats and stored Skills.
private void SetData() {
// Every Stat calculator according to Level and Base proficiency of the character's data.
// NAHits "translates" to Natural Hits according to their level.
// Ceiling to the nearest integer so that even if the character is Level 1 it will
NAHits = Mathf.CeilToInt(Level / 10f);
}
From there each Command, like Attack in this case, has the following class, CommandManager, that manages each individual Command with a ScriptableObject of CommandData that contains the metadata of each command, from type to even number of hits if it is an Attack type of command.
private int intHits;
public void Start()
{
if (commandData)
{
if (commandData.intStrikes > 0)
{
// If CommandData reads that it has more than 0 strikes then CommandManager's Hits
// will have the CommandData's numbrer of strikes.
intHits = commandData.intStrikes;
}
else
{
// If CommandData reads 0 or less strikes then CommandManager
// will read Natural Hits from it's owner with a CharacterManager script,
// which depend on the Character's Level.
intHits = CommandOwner.NAHits;
}
Debug.Log("Action registered strikes : " + this.intHits + " for " + CommandOwner.charID);
}
}
In this situation, 2 playable characters of Level 40 have a generic Attack command with a CommandManager, which reads from the same generic (intended to be used across all Playable Characters) ScriptableObject CommandData named Attack with 0 intStrikes.
PC1 manages to get the expected behavior to work, in which PC1's Attack command will have 4 hits (40/10 = 4, thus 4 hits).
PC2, however, doesn't behave as expected and even though its Attack command has the same generic CommandData as PC1's Attack command with 0 intStrikes (which should make PC2's CommandManager for regular Attack have 4 hits, it instead has 0 hits), as seen in the Console's Log.
I have not been able to identify the reason for this oddly specific bug, but I hope that this can have an explanation and a fix.
Your answer
Follow this Question
Related Questions
Missing Monobehavior after switched OS from Windows to Mac 1 Answer
Fix for strange black squares over trees when using blur? 4 Answers
when pressing play gameobject get deleted and the game breaks 0 Answers
Object Rotating Randomly 1 Answer
Why do I keep getting UNetWeaver error: GetWriteFunc recursion depth exceeded for NetworkInstanceId 2 Answers