Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[SOLVED] Cannot implicitly convert type 'System.Collections.GEnric.List<Trial>' to 'Trial'

Discussion in 'Scripting' started by RoseEmber, Aug 6, 2019.

  1. RoseEmber

    RoseEmber

    Joined:
    Jan 14, 2019
    Posts:
    11
    [SOLVED]
    I have CSV file filled with data that I read in a script and store in a dynamic array.
    I'm having trouble with a method (getNextTrial) where I can go to the next line of data in the list and may extract to change the weather or scene in my game for example.

    Code (CSharp):
    1. public class Trial
    2. {
    3.     public int TrialNum;
    4.     public string status;
    5.     public string weather;
    6.     public int time;
    7.     public int duration;
    8.     public string scene;
    9.     public string displayType;
    10.     public string InputType;
    11.  
    12.  
    13. }
    14.  
    15. public class FollowTrial : MonoBehaviour
    16.     {
    17.         public static FollowTrial instance;
    18.         public string path;
    19.  
    20.  
    21.     public static class ExpTrials {
    22.  
    23.         private static List<Trial> trialList;
    24.         private static int counter;
    25.         private static Trial t;
    26.  
    27.         public class ReadCSV : MonoBehaviour //reads data and populates list
    28.         {
    29.  
    30.  
    31.             public static List<Trial> ParseFromString(string file_contents)
    32.             {
    33.                 //List<Trial> Trials = new List<Trial>();
    34.                 string[] row = file_contents.Split('\n');
    35.  
    36.                 for (int i = 1; i < row.Length; i++)
    37.                 {
    38.                     string[] column = row[i].Split(',');
    39.                     Trial t = new Trial();
    40.  
    41.                     int.TryParse(column[0], out t.TrialNum);
    42.                     t.status = column[1];
    43.                     t.weather = column[2];
    44.                     int.TryParse(column[3], out t.time);
    45.                     int.TryParse(column[4], out t.duration);
    46.                     t.scene = column[5];
    47.                     t.displayType = column[6];
    48.                     t.InputType = column[7];
    49.  
    50.                    trialList.Add(t);
    51.                 }
    52.  
    53.                 counter = 0;
    54.                 return trialList;
    55.             }
    56.  
    57.         }
    58.  
    59.         public static Trial getNextTrial(List<Trial> t)
    60.         {
    61.             t = trialList[counter] ;// get trial from list and store at t
    62.             trialList.RemoveAt(counter); //remove trial from list
    63.             counter++;
    64.          
    65.             return t;
    66.         }
    Thank you in advanced.
     
    Last edited: Aug 6, 2019
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,513
    your problem is here:
    Code (csharp):
    1.  
    2.         public static Trial getNextTrial(List<Trial> t)
    3.         {
    4.             t = trialList[counter] ;// get trial from list and store at t
    'trialList' is a list of objects of type Trial. So when you say 'trialList[counter]' you're returning a 'Trial' object. But 't' is a variable of type 'List<Trial>'.

    You're assigning a Trial to a variable of type List<Trial>.

    Those are not the same types.

    Hence the error:
    The error says exactly what the problem is... can't convert between those 2 types.

    It also doesn't help you have a class level field named 't' of type 'Trial', and a parameter named 't' of type 'List<Trial>' which probably adds even more confusion to the code.

    I'm also noticing you have another variable in 'ParseFromString' also named 't'... also unnecessarily nested classes... and in general your code is just very confusing to read. It should be pointed out that short variable names like 't' are usually frowned upon for this very reason.
     
  3. RoseEmber

    RoseEmber

    Joined:
    Jan 14, 2019
    Posts:
    11

    Oh wow, realized what I needed to do lol

    Code (CSharp):
    1. public static Trial getNextTrial()
    2.         {
    3.             currentTrial = trialList[counter] ;// get trial from list and store at t
    4.             trialList.RemoveAt(counter); //remove trial from list
    5.             counter++;
    6.            
    7.             return currentTrial;
    8.         }
    Ty!!