Search Unity

"Cannot Conver from <string> to 'string' ??? List and Scripting Newbie...

Discussion in 'Scripting' started by RDG_Admin, Aug 16, 2019.

  1. RDG_Admin

    RDG_Admin

    Joined:
    Oct 4, 2014
    Posts:
    22
    So i'm trying to enable a couple boolean values depending on what is entered in the two text fields (WindowsEditionCheck and GPUMakeModelText). I want it to look at what is entered and look at the list I have and enable the booleans under the if statement however the error:

    cannot convert from System.Collections.Generic.List<string> to 'string'


    I have absolutely no idea what this means... the full code is below...

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class configCheck : MonoBehaviour
    6. {
    7.  
    8.     public Text WindowsEditionCheck = null;
    9.     public Text GPUMakeModelText = null;
    10.     public bool WinEd_10_64 = false;
    11.     public bool Nvidia_Current = false;
    12.    
    13.     public List<string> currentNvidiaGPU = new List<string>();
    14.  
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         currentNvidiaGPU.Add("NVIDIA GeForce RTX 2080 Ti");
    20.         currentNvidiaGPU.Add("NVIDIA GeForce RTX 2080");
    21.         currentNvidiaGPU.Add("NVIDIA GeForce RTX 2070");
    22.         WindowsEditionCheck();
    23.     }
    24.  
    25.     void WindowsEditionCheck()
    26.     {
    27.         if ((WindowsEditionText.text.Contains("Windows 10")) && (GPUMakeModelText.text.Contains(currentNvidiaGPU)))
    28.         {
    29.             WinEd_10_64 = true;
    30.             Nvidia_Current = true;
    31.         }
    32.      
    33.     }
    34.  
    35. }
    36.  
     
  2. RDG_Admin

    RDG_Admin

    Joined:
    Oct 4, 2014
    Posts:
    22
    so I've realised in the code above I've asked if the individual like contains the entire list... which is the wrong way around (Line 27) so I've flipped this....

    Code (CSharp):
    1. if ((WindowsEditionText.text.Contains("Windows 10")) && (currentNvidiaGPU.Contains(GPUMakeModelText.text)))
    This seems to have rid of the initial error, however this doesn't seem to change the boolean values at all - any ideas?
     
  3. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    The error means you're passing in a List instead of a string.
    A List of strings is still a List, so if you want to compare the values in the list, you need to reference them directly.
    You can use a loop to accomplish this:
    Code (CSharp):
    1. for(int i = 0; i < currentNvidiaGPU.Count; i++) {
    2.    if ((WindowsEditionText.text.Contains("Windows 10")) && (GPUMakeModelText.text.Contains(currentNvidiaGPU[i]))) {
    3.       //etc...
    4.    }
    5. }
    Treat Lists exactly as how you would treat arrays, because they work in the same way, except their collection of items is dynamic.
     
  4. RDG_Admin

    RDG_Admin

    Joined:
    Oct 4, 2014
    Posts:
    22
    Hey Vryken,

    Thanks for responding - I noticed after posting that:

    Code (CSharp):
    1. (GPUMakeModelText.text.Contains(currentNvidiaGPU))
    is checking if the input contains the whole list... which it obviously wouldn't. I flipped it around to...

    Code (CSharp):
    1. (currentNvidiaGPU.Contains(GPUMakeModelText.text))
    and this has gotten rid of the initial error, however this is still not flipping the boolean values.... any ideas?
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    If your values aren't returning what you expect, you need to see what you're checking against. What the value of GPUMakeModelText.text is. Remember stuff like caps matter.
     
  6. RDG_Admin

    RDG_Admin

    Joined:
    Oct 4, 2014
    Posts:
    22
    yeah the list is made up of the exact values that GPUMakeModelText.text which is pulling it's info from the

    Code (CSharp):
    1. SystemInfo.graphicsDeviceName;
    in another script... so I'm really not sure

    EDIT: I just entered my own value into the text ui and the list - and it worked... now I'm REALLY lost
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    The problem with comparing strings is you may be missing something without realizing it. Maybe a hidden character or an extra space.

    One thing to check is the length of the string you are passing in. If it's pulled from somewhere, check the length and then count what you see when you debug the value of the string and see if they match.