Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
-1
Question by samuelme · Jun 16, 2017 at 10:42 PM · listclass

Objects not being added to a list

So right now I'm trying to set up a turn based combat system, and I need to make a list for turn order. I have a list set up which is supposed to store a class called AgentInit. However, nothing is being stored in the list.

     var initiative = new List.<AgentInit>();
 
     for (var g : int = 0; g < enemyList.length; g++) {
         initiative.Add (new AgentInit (enemyList[g].GetComponent(AgentStats).speed, enemyList[g]));
         print(enemyList[g].GetComponent(AgentStats).agentName);
     }
     for (var h : int = 0; h < playerList.length; h++) {
         initiative.Add (new AgentInit (playerList[h].GetComponent(AgentStats).speed, playerList[h]));
         print(enemyList[h].GetComponent(AgentStats).agentName);
     }
 
     initiative.Sort();

That is the section of code which should be adding the objects to the list. Those lines that say to print work perfectly fine, and I've looked all over and have no clue what the problem is. (BTW the two variables stored in AgentInit are an int and a GameObject)

I'm not super experienced with code, any help is appreciated.

Comment
Add comment · Show 5
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 hexagonius · Jun 17, 2017 at 01:07 AM 0
Share

I don't know where you check the content of the list, but it's definitely not empty. Only thing I found was that your second print also looks into the enemy list, is that's confusing you.

avatar image samuelme hexagonius · Jun 17, 2017 at 09:48 PM 0
Share

It definitely is empty. The inspector says it's empty and I'm getting an error when I try to access element 0. I did fix that print statement though. Here's a picture if it helps. http://imgur.com/a/ZVekm

avatar image hexagonius samuelme · Jun 18, 2017 at 06:19 AM 0
Share

I still have no idea where you're reading the 0 index. The error says line 81. Where is that? Am I even looking at the same script? Please post the relevant parts in total. The screenshot doesn't really say anything.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Mraction2012 · Jun 18, 2017 at 09:34 PM

that line "var initiative = new List.();" should be called once, e.g in start()

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
avatar image
0

Answer by dreg_master · May 17, 2018 at 02:04 PM

For me, I needed to add my Human Class to MonoBehaviour otherwise when i instantiated a human and tried to add it, it wouldn't work. [code] using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class PopulationManager : MonoBehaviour { [SerializeField] private Image PopulationIcon; [SerializeField] private Text PopulationText; [SerializeField] private List Humans; //public string manualString;

 // Use this for initialization
 private void Start()
 {
     PopulationIcon = GetComponentInChildren<Image>();
     PopulationText = GetComponentInChildren<Text>();

     Humans.Add(new Human());
     Humans.Add(new Human());
     Humans.Add(new Human());

     foreach (Human h in Humans)
     {
         Debug.Log(h.ToString());
     }
 }

 // Update is called once per frame
 private void Update()
 {
     int i = Humans.Count;
     PopulationText.text = "" + i;
 }

} [/code]

Comment
Add comment · Show 1 · 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 dreg_master · May 17, 2018 at 02:10 PM 0
Share

[code] internal class Human : $$anonymous$$onoBehaviour { public GameObject HumanPrefab; private static int HEALTH_START; private static int HEALTH_A = 100; private static int HEALTH_B = 70; private static int HEALTH_C = 50; private static int HEALTH_D = 25; private static int HEALTH_E = 0;

 private int health;

 public Human()
 {
     health = HEALTH_START;
 }

 public override string ToString()
 {
     return base.ToString();
 }

} [/code]

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

69 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

How to find a Class by value 2 Answers

change value of prefab field in a array of prefabs 0 Answers

Inspector variables from class in List are always initialized to its default values 1 Answer

Orderrring a List Ascending or Descending C# Advanced 2 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