Search Unity

Trouble using System.IO and String.Substring...

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

  1. Mirage

    Mirage

    Joined:
    Jan 13, 2010
    Posts:
    230
    Not sure if this is a bug or not, but whenever I import "System" and "System.IO" I get a "Method not found: System.String[].Substring" error. I know my code is ok, if I delete the import System.IO the error goes away. What is going on with this and how can I use both System.IO commands and the Substring command together in the same script? Splitting System.IO and Substring elements into seperate scripts has gotten me past several conflicts, but this time it is unavoidable.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Would help if you posted some of the affected code. Importing System can cause some namespace conflicts:

    Code (csharp):
    1. var foo = Random.Range(0, 10); // Fine
    vs.

    Code (csharp):
    1. import System;
    2. var foo = Random.Range(0, 10); // Fail: is this System.Random or UnityEngine.Random?
    To solve:

    Code (csharp):
    1. import System;
    2. var foo = UnityEngine.Random.Range(0, 10); // Disambiguate
    However I don't see how System.IO would affect Substring:

    Code (csharp):
    1. import System.IO;
    2. print ("blah".Substring(1, 2)); // No prob
    Also you don't have to import anything...it doesn't "load" anything, it just makes some things shorter to type:

    Code (csharp):
    1. import System.IO;
    2. var sr = new StreamReader("someFile");
    3. var sw = new StreamWriter("anotherFile");
    4.  
    vs.

    Code (csharp):
    1. var sr = new System.IO.StreamReader("someFile");
    2. var sw = new System.IO.StreamWriter("anotherFile");
    --Eric
     
  3. Mirage

    Mirage

    Joined:
    Jan 13, 2010
    Posts:
    230
    Thanks, that solved the problem.