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

Question Accessing csv columns from a list

Discussion in 'Scripting' started by Drachensunde, Oct 25, 2022.

  1. Drachensunde

    Drachensunde

    Joined:
    Sep 27, 2022
    Posts:
    8
    Hello,

    Example of csv data:
    CharacterName, Gender, WeaponType, FightingStyle
    someName1, male, sword, knight
    someName2, female, staff, mage
    someName3, male, dagger, assassin

    I have read in a csv and stored each column in a list and I am having trouble trying to access them. I was hoping someone could point me in the right direction.

    In unity I created an image that looks like a card with multiple text fields in it and I am trying to populate multiple cards and have the text data filled from the csv data.

    Below I have 3 different scripts

    code for card, script is attached to card and fields set:
    Code (CSharp):
    1. public class OneSingleCard : MonoBehaviour
    2. {
    3.     [SerializeField] TextMeshProUGUI CharacterName;
    4.     [SerializeField] TextMeshProUGUI Gender;
    5.     [SerializeField] TextMeshProUGUI WeaponType;
    6.     [SerializeField] TextMeshProUGUI FightingStyle;
    7.     [SerializeField] Image Background;
    8.  
    9.     public void SetCharacterNameText(string characterName)
    10.     {
    11.         CharacterName.text = characterName;
    12.     }
    13.  
    14.     public void SetGenderText(string gender)
    15.     {
    16.         Gender.text = gender;
    17.     }
    18.  
    19.     public void SetWeaponTypeText(string weaponType)
    20.     {
    21.         WeaponType.text = weaponType;
    22.     }
    23.  
    24.     public void SetFightingStyleText(string fightingStyle)
    25.     {
    26.         FightingStyle.text = fightingStyle;
    27.     }
    28.  
    29.     public void SetImageBackground(Sprite sprite)
    30.     {
    31.        Background.sprite = sprite;
    32.     }
    33.  
    34. }
    defining list columns, not attached to anything:
    Code (CSharp):
    1. public class Character
    2. {
    3.  
    4.     string characterName;
    5.     string gender;
    6.     string weaponType;
    7.     string fightingStyle;
    8.  
    9.     public Character(string rowData)
    10.     {
    11.  
    12.         string[] data = rowData.Split('\u002C');
    13.  
    14.         // parse data into properties
    15.         this.characterName = data[0];
    16.         this.gender = data[1];
    17.         this.weaponType = data[2];
    18.         this.fightingStyle = data[3];
    19.     }
    20.  
    21. }
    This last code I have on a empty game object to populate the card. I'm not sure how to get the entry.Set loop to populate the column text. I'm not quite sure where I went wrong.

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.IO;
    4. using System.Linq;
    5. using UnityEngine;
    6.  
    7. public class CharacterStatusCard : MonoBehaviour
    8. {
    9.     public OneSingleCard CharacterStatus;
    10.  
    11.     OneSingleCard MakeFreshCopyOfCharacterStatus()
    12.     {
    13.         // Create it and simultaneously parent it to the same place in UI
    14.         var copy = Instantiate<OneSingleCard>(CharacterStatus, CharacterStatus.transform.parent);
    15.  
    16.         // make it visible
    17.         copy.gameObject.SetActive(true);
    18.      
    19.         return copy;
    20.     }
    21.  
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.         // turn off example
    26.         CharacterStatus.gameObject.SetActive(false);
    27.  
    28.  
    29.         string[] csvLines = File.ReadAllLines(@"path");
    30.  
    31.         var characters = new List<Character>();
    32.  
    33.         for (int i = 1; i < csvLines.Length; i++)
    34.         {
    35.             Character character = new Character(csvLines[i]);
    36.             characters.Add(character);
    37.         }
    38.  
    39.         for(int i = 0; i < characters.Count; i++)
    40.         {
    41.  
    42.             var entry = MakeFreshCopyOfCharacterStatus();
    43.             entry.SetCharacterNameText();
    44.             entry.SetGenderNameText();
    45.             entry.SetWeaponTypeNameText();
    46.             entry.SetFightingStyleText();
    47.         }
    48.      
    49.     }
    50.  
    51. }
     
    Last edited: Oct 25, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)
     
  3. Drachensunde

    Drachensunde

    Joined:
    Sep 27, 2022
    Posts:
    8
    Hello Kurt, I was using your example from https://forum.unity.com/threads/how...a-local-drive-file-path.1341545/#post-8496575 sorry for not having that added and as far as the list being set up I used a c# video I found on youtube
    .

    However I have removed the 2nd file and changed the character status script and it works like I want it to but my csv has a lot of rows and it makes it very slow. Would you know of a more efficient way to handle it?


    Code (CSharp):
    1. public class OneSingleCard : MonoBehaviour
    2. {
    3.     [SerializeField] TextMeshProUGUI CharacterName;
    4.     [SerializeField] TextMeshProUGUI Gender;
    5.     [SerializeField] TextMeshProUGUI WeaponType;
    6.     [SerializeField] TextMeshProUGUI FightingStyle;
    7.     [SerializeField] Image Background;
    8.     public void SetCharacterNameText(string characterName)
    9.     {
    10.         CharacterName.text = characterName;
    11.     }
    12.     public void SetGenderText(string gender)
    13.     {
    14.         Gender.text = gender;
    15.     }
    16.     public void SetWeaponTypeText(string weaponType)
    17.     {
    18.         WeaponType.text = weaponType;
    19.     }
    20.     public void SetFightingStyleText(string fightingStyle)
    21.     {
    22.         FightingStyle.text = fightingStyle;
    23.     }
    24.     public void SetImageBackground(Sprite sprite)
    25.     {
    26.        Background.sprite = sprite;
    27.     }
    28. }
    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.IO;
    4. using System.Linq;
    5. using UnityEngine;
    6. public class CharacterStatusCard : MonoBehaviour
    7. {
    8.     public OneSingleCard CharacterStatus;
    9.     OneSingleCard MakeFreshCopyOfCharacterStatus()
    10.     {
    11.         // Create it and simultaneously parent it to the same place in UI
    12.         var copy = Instantiate<OneSingleCard>(CharacterStatus, CharacterStatus.transform.parent);
    13.         // make it visible
    14.         copy.gameObject.SetActive(true);
    15.    
    16.         return copy;
    17.     }
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         // turn off example
    22.         CharacterStatus.gameObject.SetActive(false);
    23.         string[] csvLines = File.ReadAllLines(@"path");
    24.         foreach(string csvLine in csvLines)
    25.         {
    26.             var entry = MakeFreshCopyOfCharacterStatus();
    27.             entry.SetCharacterNameText();
    28.             entry.SetGenderNameText();
    29.             entry.SetWeaponTypeNameText();
    30.             entry.SetFightingStyleText();
    31.         }
    32.    
    33.     }
    34. }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    I don't think that's what is making it slow.

    Why do you think that is? If you think it is, cut your spreadsheet down to 1 line and try.

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    When in doubt, print it out!(tm)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.