Search Unity

CSV data and properties

Discussion in 'Scripting' started by Stefanovicz, Apr 16, 2018.

  1. Stefanovicz

    Stefanovicz

    Joined:
    Jan 28, 2015
    Posts:
    57
    Thanks for your answer. The debug points at another script (SetColor lines 55 and 63). But I didn't touch it. That's why I am confused.

    Really, it worked perfectly fine a couple of hours ago. The problem occured just after I changed the csv.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Linq;
    5.  
    6. [RequireComponent(typeof(PolygonCollider2D))]
    7.  
    8. public class SetColor : MonoBehaviour
    9. {
    10.     private SpriteRenderer sprite;
    11.  
    12.     private Color32 hoverColor;
    13.     private Color32 startColor;
    14.  
    15.     void Start()
    16.     {
    17.         ColorManager colorManager = FindObjectOfType<ColorManager> ();
    18.         CSVReader csvReader = FindObjectOfType<CSVReader> ();
    19.  
    20.  
    21.         if (!colorManager)
    22.         {
    23.             Debug.Log ("colorManager is NULL");
    24.             return;
    25.         }
    26.  
    27.         if (!csvReader) {
    28.             Debug.Log ("csvReader is NULL");
    29.             return;
    30.         }
    31.  
    32.         foreach (Region region in csvReader.regions)
    33.         {
    34.             if(region.Pays == gameObject.transform.parent.name)
    35.             {
    36.                 if(region.color == Color.black)
    37.                 {
    38.                     startColor = new Color (Random.value, Random.value, Random.value, 0.5f);
    39.                     if (!colorManager.colors.Contains (startColor) && startColor != new Color(255,255,255,1))
    40.                     {
    41.                         colorManager.colors.Add (startColor);
    42.                         region.color = startColor;
    43.                     }
    44.                 }
    45.                 sprite = GetComponent<SpriteRenderer> ();
    46.                 sprite.color = region.color;
    47.             }
    48.         }
    49.     }
    50.  
    51.  
    52.     void OnMouseEnter()
    53.     {
    54.         hoverColor = new Color (255, 255, 255, 1);
    55.         startColor = sprite.color;
    56.         sprite.color = hoverColor;
    57.     }
    58.  
    59.  
    60.  
    61.     void OnMouseExit()
    62.     {
    63.         sprite.color = startColor;
    64.     }
    65.  
    66.     /*
    67.     void OnMouseDown()
    68.     {
    69.         sprite.color = new Color (255, 255, 255, 1);
    70.  
    71.  
    72.     }
    73.  
    74.     void OnMouseUp()
    75.     {
    76.         sprite.color = startColor;
    77.     }
    78.     */
    79. }
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,776
    Put debug before line 55 and 63, and check what startColor is? Doe it has color format?
     
  3. Stefanovicz

    Stefanovicz

    Joined:
    Jan 28, 2015
    Posts:
    57
    Here is something else: Capture.PNG
    Something seems broken in the directories (access denied)
     
  4. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Do you still have a file called DataCSV.csv in the Resources folder? The script looks the same from a few months ago.
    but the error is in the setColor script. can you post an updated script?
     
  5. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    you're SetColor script is working in my test scene.
     
  6. Stefanovicz

    Stefanovicz

    Joined:
    Jan 28, 2015
    Posts:
    57
    Hello johne5 !

    The SetColor and CSVReader I just uploaded are the updated ones.

    The thing is that I realised that there was a mistake in the csv so I changed it. But since, the colors don't work anymore and even in the CSVReader script, the count shows 0.

    So I tried replacing the csv file with a new, corrected one and with the same name. But nothing.
     
  7. Stefanovicz

    Stefanovicz

    Joined:
    Jan 28, 2015
    Posts:
    57
    yes, it worked a couple hour ago. I messed something with the csv.
     
  8. Stefanovicz

    Stefanovicz

    Joined:
    Jan 28, 2015
    Posts:
    57
    yes, I think the problem comes from the csv. I did not change the SetColor script and it worked.
     
  9. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    here is my csv file format
    Code (CSharp):
    1. Cali,United States,423967,39250,1.007,21.82,1.0021,anglais,protestant
    2. Mex,Mexico,103900,30250,1.06,10.12,5.0021,mexais,protestant
    3. Canada,Canada,47800,88100,7.03,66.24,4.044,canais,protestant
    4. Euro,Euro,9993967,399990,9.007,99.82,99.0021,eruais,protestant
     
  10. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    This is my actual file that's in the Resources folder.
    What was the screen shot of?
     

    Attached Files:

  11. Stefanovicz

    Stefanovicz

    Joined:
    Jan 28, 2015
    Posts:
    57
    Sorry for the previous post, my CSV is like this:
    Code (CSharp):
    1. Nom;Pays;Superficie;Population;Croissancepopulation;PIB;CroissancePIB;Langue;Religion
    2. Badakhshan;Afghanistan;44936;1000;1.023;1;1.0016;tadjik;islam-sunnite
    3. Kaboul;Afghanistan;75571;10200;1.023;11;1.0016;pachto;islam-sunnite
    4. Hazaristan;Afghanistan;106414;3700;1.023;4;1.0016;hazara;islam-sunnite
    5. Mazar-i-Sharif;Afghanistan;118453;7000;1.023;8;1.0016;turkmène;islam-sunnite
    6. Herat;Afghanistan;123840;2900;1.023;3;1.0016;pachto;islam-sunnite
    7. Kandahar;Afghanistan;183650;3000;1.023;3;1.0016;pachto;islam-sunnite
    Is it an issue with commas and semicolons ?
     
  12. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,776
    I still wonder, why color is the error line. You should check this out, as it may give a clue.
    generate again csv file from excel.
    Maybe tehere is issue with line ending.
     
  13. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    yah, can you make sure that the Sprint Render is still active on the image

    I still thinks it's csv, because it has a length of 0
     
  14. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    in the Start() of SetColor. it's looking for the CSVReader script and then finds a color value and sets it's color to the local Color "startColor". In the OnMouseEnter this is null when trying to access it. it didn't find it's name in the CSVReader.regions list. so it doesn't set it.
     
  15. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,776
    1. Get back to previous file version. Start from there
    2. Generate file again
    3. Ensure you script never results with null values, which can cause critical error. You need exception handling.
    4. Along with exceptions, put appropriate message for debug.
     
  16. Stefanovicz

    Stefanovicz

    Joined:
    Jan 28, 2015
    Posts:
    57
    okay, okay.... so first of all, thanks to both of you !

    I generated a new csv file and after some time I discovered that I have to export as UTF-8 comma separated with Excel (even though Unity read it as semicolon) but whatever... it works ! The CSV was indeed the problem (the color stuff works just fine).

    But I still have 2 minor issues:

    1. When I modify something in Excel and then I open Unity, the columns of the CSV merge together. I have to close everything, reopen Excel and convert the data to separate them again and then back to Unity. I don't know why.

    2. Another strange issue... when I move the camera, the colours change slightly (they are lighter when the x axis is negative, and darker when the x axis is positive). It's more like the global luminosity actually, not really the colours.

    On the picture, the debug shows the area (=Superficie) to 0 for every region, but I corrected it.

    Thanks again :)
     

    Attached Files:

    Last edited: Aug 26, 2018
  17. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    when you're in excel, make sure you always export to csv, not just clicking the save button.
     
  18. Stefanovicz

    Stefanovicz

    Joined:
    Jan 28, 2015
    Posts:
    57
    ooh ok I didn't know that, thanks again !