Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Aug 22, 2014 at 06:52 AM by HexadimensionalerAlp for the following reason:

Found solution

avatar image
0
Question by HexadimensionalerAlp · Aug 17, 2014 at 08:09 AM · c#listfor-loopadd

[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]);
             }
         }
     }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HexadimensionalerAlp · Aug 17, 2014 at 09:43 AM 0
Share

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));
     }
 }
 
avatar image boop5 · Aug 17, 2014 at 01:29 PM 0
Share

Does Skill added [..] gets printed?

2 Replies

  • Sort: 
avatar image
0

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 :)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HexadimensionalerAlp · Aug 19, 2014 at 11:46 AM 0
Share

Thank you very much, I'll try it your way. Seems like a better/easier way :)

avatar image Alessio89 · Aug 20, 2014 at 12:58 PM 1
Share

Well the important thing is you figured it out :) Have fun with your poject!

avatar image HexadimensionalerAlp · Aug 22, 2014 at 06:51 AM 0
Share

Thank you :D

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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

C# Adding Multiple Elements to a List on One Line 5 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges