I am trying to read different text files based on the index of the current scene (in Unity lingo). So, I decided to use a switch statement as follows:
void MyReadString()
{
Scene currentScene = SceneManager.GetActiveScene(); // Unity command to get the current scene info
int buildIndex = currentScene.buildIndex; // Unity command to get the current scene number
string path = string.Empty; // Create an empty string variable to hold the path information of text files
switch (buildIndex)
{
case 0:
string path = "Assets/Scripts/some.txt"; //It says here that the variable path is assigned but never used!
break;
case 1:
string path = "Assets/Scripts/another.txt"; //Same here - assigned but never used.
break;
// and many other cases - atleast 6 more
}
StreamReader reader = new StreamReader(path); // To read the text file
// And further string operations here - such as using a delimiter, finding the number of values in the text etc
}
If I comment out the line:
string path = string.Empty;
then,
StreamReader reader = new StreamReader(path); // says here the name "path" does not exist in the current context.
I know this has to do something with the scope of the switch statement. However, declaring the string variable outside the switch as an "Empty" is not working. Kindly let me know if I can assign a string value within a Switch statement and use that value later on. Or if it is not possible, please suggest me workarounds.