Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Making Object get values from a list that derives from a class

Discussion in 'Scripting' started by Santhys, Dec 21, 2015.

  1. Santhys

    Santhys

    Joined:
    Jul 15, 2015
    Posts:
    3
    I been trying to make an object call a list specific index to a var, this list as couple variables in a class like:

    Code (JavaScript):
    1. ///// PlayerData.js
    2. #pragma strict
    3.  
    4. import System.Collections.Generic;
    5.  
    6. public class PData ///// PData is my class that contains the var that i want to display
    7. {
    8.     //variaveis de idetificaçao
    9.     public var ID : int;
    10.     public var Name : String;
    11.    
    12.     //variaveis de status
    13.    
    14.     public var HitPoint = new int[2];
    15.     public var SoulPoint = new int[2];
    16.  
    17. }
    18.  
    19.  
    20. //Container foi the list PData
    21. public class PlayersData {
    22.  
    23.     public var Players : List.<PData> = new List.<PData>(); // This is the list that contain class
    24.    
    25. }
    26.  
    27.  
    and i tried making a var to give the index value to retrieve the information from this list, but i the list returns blank or give me error =/

    Code (JavaScript):
    1. ///// Identify.js
    2. #pragma strict
    3.  
    4. public var ID : int;
    5.  
    6. public var Status = PlayersData.Players[ID];
    and i been wondering if there a way i make a link between these specific index of the list and that var, for example:

    public var Status = PlayersData.Players[ID]; return the value value from the index 0
    and
    PlayersData.Players[ID] = Status; save the modifications to the original
     
  2. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    you have lots of different options with list.
    - you can sort the list depending on the variable's value so that it sit first or last in your list.
    - you can get the index of an element by using list.IndexOf(element);
    - you can iterate through your list looking for the element you want by checking some variables value.
    - ...
    it all depends on what you need...
    your list returns blank 1) your never put anything in it 2) you never specify a value for ID (so it goes to index 0 of an empty list, which returns error).
     
  3. Santhys

    Santhys

    Joined:
    Jul 15, 2015
    Posts:
    3
    Sorryy, yes, in the use i using the list like this:

    Code (JavaScript):
    1. public var playerdata = new PlayersData();
    then i assing the var values from the inspector.
    So i been wondering if the instance of that list is in that var, but if i use;

    Code (JavaScript):
    1. public var Status = myScript.playerdata.Players[ID];
    I get a error, so i ask if u can say where i wrong >.<

    In that case, the instance var that hold the list, how can i get acess to that specific instance?