- Home /
Found solution
[C#] Programm does not add to list
I have the Problem that when I call the AddSkill void it is called properly but it doesn't add the skill to the Skills list. Here is My code:
public List<Skill> Skills = new List<Skill>();
private SkillDatabase skillDatabase;
void Start ()
{
skillDatabase = GameObject.FindGameObjectWithTag("Skill Database").GetComponent<SkillDatabase>();
AddSkill (1);
AddSkill (2);
AddSkill (3);
}
private void AddSkill (int id)
{
print ("AddSkill called");
for (int j = 0; j < skillDatabase.skills.Count; j++)
{
print ("Skill number" + j);
if (skillDatabase.skills [j].skillID == id)
{
print ("Skill added: " + skillDatabase.skills[j].skillName);
Skills.Add(skillDatabase.skills[j]);
}
}
}
And here is the Database:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SkillDatabase : $$anonymous$$onoBehaviour {
public List<Skill> skills = new List<Skill>();
void Start()
{
//skills.Add(new Skill(name, ID, pointsNeeded, stageNeeded, description, skill.skilltype.type, known))
skills.Add(new Skill("Survival 1", 1, 1, 0, "Basic Survival - You don't get \n hungry/thirsty that much anymore \n and you are less likely ill \n - needed for Survival-Skills of stage 1", Skill.SkillType.Survival, false));
skills.Add(new Skill("Survival 2", 2, 2, 1, "Basic Survival - You don't get \n hungry/thirsty that much anymore \n and you are less likely ill \n - needed for Survival-Skills of stage 2", Skill.SkillType.Survival, false));
skills.Add(new Skill("Survival 3", 3, 3, 2, "Basic Survival - You don't get \n hungry/thirsty that much anymore \n and you are less likely ill \n - needed for Survival-Skills of stage 3", Skill.SkillType.Survival, false));
}
}
Answer by Alessio89 · Aug 17, 2014 at 01:18 PM
First of all, if you ask me, I'd suggest you use a Dictionary instead of a list. Much easier to get the corresponding skill like so: Skills["Survival 1"]. No need to manually iterate through the list and find the right Id. Dictionaries are build to optimize these kind of "Here's the key, gimme my value" process.
I'd go like this:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SkillDatabase : MonoBehaviour {
public Dictionary<string, Skill> skills = new Dictionary<string, Skill>();
void Start()
{
//skills.Add(new Skill(name, ID, pointsNeeded, stageNeeded, description, skill.skilltype.type, known))
skills.Add("Survival 1", new Skill("Survival 1", 1, 1, 0, "Basic Survival - You don't get \n hungry/thirsty that much anymore \n and you are less likely ill \n - needed for Survival-Skills of stage 1", Skill.SkillType.Survival, false));
skills.Add("Survival 2", new Skill("Survival 2", 2, 2, 1, "Basic Survival - You don't get \n hungry/thirsty that much anymore \n and you are less likely ill \n - needed for Survival-Skills of stage 2", Skill.SkillType.Survival, false));
skills.Add("Survival 3", new Skill("Survival 3", 3, 3, 2, "Basic Survival - You don't get \n hungry/thirsty that much anymore \n and you are less likely ill \n - needed for Survival-Skills of stage 3", Skill.SkillType.Survival, false));
}
}
And then use it like so:
public List<Skill> Skills = new List<Skill>();
private SkillDatabase skillDatabase;
void Start ()
{
skillDatabase = GameObject.FindGameObjectWithTag("Skill Database").GetComponent<SkillDatabase>();
AddSkill ("Survival 1");
AddSkill ("Survival 2");
AddSkill ("Survival 3");
}
private void AddSkill (string name)
{
print ("AddSkill called");
// Make sure that the skill actually exists:
if (skillDatabase.skills.ContainsKey(name))
{
Skills.Add(skillDatabase.skills[name]);
}
}
Now, that doesn't actually answer your question, so after my humble opinion, if you still want to use your version with the List, I'd like to know if the "print" lines are fireing, if so with what results :)
Thank you very much, I'll try it your way. Seems like a better/easier way :)
Well the important thing is you figured it out :) Have fun with your poject!
Answer by HexadimensionalerAlp · Aug 19, 2014 at 01:50 PM
It didn't work either but I discovered that it was because of the scriptexecutionorder
Follow this Question
Related Questions
List That Won't Remove The Last Two Elements 3 Answers
How to Convert FileInfo to TextAsset to add to List 1 Answer
How do I add a float variable into a list ? 2 Answers
For loop does not loop C# 2 Answers