- Home /
Problem with adding items to a list
I'm trying to make a database of enemies for my turn based fighting game. So far I tried two things and neither worked, but I feel like this way is the better approach. If you know another method to make a database (preferably without an external source) please include it in your answer. This is my code for the database:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyDatabase : MonoBehaviour {
public List<Enemy> enemyList = new List<Enemy>();
// Use this for initialization
void Start () {
//ENEMY CREATION
Enemy e0 = new Enemy ();
e0.enemyLevel = 1;
e0.maxHealthPoints = 200;
e0.maxManaPoints = 100;
e0.minAtk = 10;
e0.maxAtk = 20;
e0.minMAtk = 0;
e0.maxMAtk = 0;
e0.phPenetration = 0;
e0.magPenetration = 0;
e0.armor = 0;
e0.magRes = 0;
e0.baseTime = 1;
e0.critChance = 0;
e0.critMult = 0;
e0.blockChance = 0;
e0.blockAmount = 0;
e0.evadeChance = 0;
e0.hitRate = 0;
e0.lifeSteal = 0;
e0.manaSteal = 0;
e0.healthRegen = 0;
e0.manaRegen = 0;
e0.soulValue = 5;
e0.goldValue = 0;
e0.itemOdds = 0;
e0.itemRarity = 0;
enemyList.Add (e0);
}
// Update is called once per frame
void Update () {
}
}
And this is where I use it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class CurrentEnemy : MonoBehaviour
{
public EnemyDatabase databaseEnemy;
public int enemyLevel;
public int soulValue; //determines soul points you get from killing it
public int goldValue; //determines gold you get from killing it
public int itemOdds; //determines the odds of getting an item
public int itemRarity; //determines the rarity of dropped item
public float maxHealthPoints;
public float healthPoints;
public int maxManaPoints;
public int manaPoints;
public int minAtk, maxAtk;
public int minMAtk, maxMAtk;
public int phPenetration;
//reduces oponents armor
public int magPenetration;
//reduces oponents magic resist
public int armor, magRes;
public int aSpeed; //items
public int aSpeedMult; //skills, passives
public float baseTime; //weapon
//base interval of attacks
public float atkTimer;
//atkTimer=base*aCoef
//>100 => %
public int critChance;
//float critChance 0-100
public int critMult;
public int blockChance;
//Same as crit
public int blockAmount; //shield, weapon
//0-100
public int evadeChance;
public int hitRate;
//reduces oponents evasion
public int lifeSteal, manaSteal; //skills, passives, items
public int healthRegen;
public int manaRegen;
public void LoadEnemy(){
int index=0;
Debug.Log (databaseEnemy.enemyList.Count ());
for (int i = 0; i < databaseEnemy.enemyList.Count (); i++) {
if (databaseEnemy.enemyList [i].enemyLevel == enemyLevel)
index = i;
}
Debug.Log (index);
maxHealthPoints = databaseEnemy.enemyList [index].maxHealthPoints;
maxManaPoints = databaseEnemy.enemyList [index].maxManaPoints;
minAtk = databaseEnemy.enemyList [index].minAtk;
maxAtk = databaseEnemy.enemyList [index].maxAtk;
minMAtk = databaseEnemy.enemyList [index].minMAtk;
maxMAtk = databaseEnemy.enemyList [index].maxMAtk;
phPenetration = databaseEnemy.enemyList [index].phPenetration;
magPenetration = databaseEnemy.enemyList [index].magPenetration;
armor = databaseEnemy.enemyList [index].armor;
magRes = databaseEnemy.enemyList [index].magRes;
baseTime = databaseEnemy.enemyList [index].baseTime;
critChance = databaseEnemy.enemyList [index].critChance;
critMult = databaseEnemy.enemyList [index].critMult;
blockChance = databaseEnemy.enemyList [index].blockChance;
blockAmount = databaseEnemy.enemyList [index].blockAmount;
evadeChance = databaseEnemy.enemyList [index].evadeChance;
hitRate = databaseEnemy.enemyList [index].hitRate;
lifeSteal = databaseEnemy.enemyList [index].lifeSteal;
manaSteal = databaseEnemy.enemyList [index].manaSteal;
healthRegen = databaseEnemy.enemyList [index].healthRegen;
manaRegen = databaseEnemy.enemyList [index].manaRegen;
soulValue = databaseEnemy.enemyList [index].soulValue;
goldValue = databaseEnemy.enemyList [index].goldValue;
itemOdds = databaseEnemy.enemyList [index].itemOdds;
itemRarity = databaseEnemy.enemyList [index].itemRarity;
}
void Start ()
{
enemyLevel = 1;
LoadEnemy ();
}
// Update is called once per frame
void Update ()
{
if (healthPoints < 0)
healthPoints = 0;
}
}
I'm getting the following console error: I think my List is empty. I followed a guide where the person initialized the List like this: public List enemyList = new List(); And the enemies : Enemy e0 = new Enemy (); I don't know if this is right, but it worked for him. Again, if you know a better approach, please share. Thank you very much!
Answer by dev-waqas · Jan 18, 2018 at 08:47 PM
Most probably its because of script execution order. You want to make sure your script CurrentEnemy
should execute after EnemyDatabase
so you should set order of scripts in following window.
It did fix the problem, thank you very much! Are there any other important bits like this that I should know before committing to a large project?
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Saving Data (closed) 1 Answer
how to split TextAsset into a Dictionary? 0 Answers
List of custom classes in MonoBehaviour's Data gets lost after Re-Opening Unity!!! 0 Answers
Enemy list wont update 0 Answers