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

Method not found-Substring Error

Discussion in 'Scripting' started by Mirage, Apr 16, 2010.

  1. Mirage

    Mirage

    Joined:
    Jan 13, 2010
    Posts:
    230
    Not sure what is going on here, I looked at it for awhile and cant see anything wrong. The attached code(partialy completed) is supposed to load the contents of a text file and find the characters bounded by double quotes. I have the method down in my head, I just cant get past the Method not found: System.String[].Substring error.

    Code (csharp):
    1.  
    2. function LoadFile(pathname)
    3. {
    4.     var fileContents = System.IO.File.ReadAllLines(pathname+".txt");
    5.     fileContents.ToString();
    6.     Debug.Log(fileContents.GetType());
    7.     for(var j = 0; (fileContents.length-1); j++)
    8.     {
    9.         if ((fileContents.Substring(j, 1)) == "\""  Mathf.Pow(-1,j) == 1)//This is where the error occurs
    10.         {
    11.             tempArray.Push(fileContents.Substring(j+1, 1)); // I imagine it will happen here too
    12.         }
    13.     }
    14.     Debug.Log(tempArray);
    15. }
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    ReadAllLines returns System.String[] (i.e., an array). "fileContents.ToString()" isn't doing anything, since ToString() returns a value and you aren't assigning that value to anything. Even if you did, it would still end up as a string array. The error is because you can't get a substring from an array, it has to be from a single string, so you'd loop through all entries in the array.

    --Eric