- Home /
I'm having .ToString Problems
When I try to use .ToString, Unity gives me this error message: Cannot convert 'String' to 'int'. Why?
Here's my code so I don't have to edit it in:
newD1 = newD1.ToString();
Also: I have this line of code that compares the index of this array to a string. The value in the array and the variable are both strings. Yet...
if(monsters[i] == loadEnemyNameToString) {
}
That's giving me this error:
Cannot cast from source type to destination type.
Would you be so kind as to help me again? This problem is giving me eternal frustration.
Oh, and here's what's in the array:
var monsters = fileContents.Split("-"[0]);
Ah what the heck. Here's the entire function that's such a pain:
function SearchLog () { var sr = new StreamReader(Application.dataPath + "/" + readFilePath); var fileContents = sr.ReadToEnd(); sr.Close();
var monsters = fileContents.Split("-"[0]);
for (var i in monsters) {
if(monsters[i] == loadEnemyName.ToString()) {
var i2 = i + 1;
var i3 = i + 2;
var i4 = i + 3;
var i5 = i + 4;
newEnemyName = monsters[i];
var loadedEnemyHealth = monsters[i2];
var loadedEnemyDefense = monsters[i3];
var loadedD1 = monsters[i4];
var loadedD2 = monsters [i5];
newEnemyHealth = float.Parse(loadedEnemyHealth);
newEnemyDefense = float.Parse(loadedEnemyDefense);
newD1 = float.Parse(loadedD1);
newD2 = float.Parse(loadedD2);
}
}
}
I'm sure there are much more elegant ways of doing it but I'm on my last bit of juice. At this point I'm just nailing technical looking stuff together.
What I'm doing is loading a file that contains monster attributes. I'm then trying to parse it and split up the file at the - to get an array that contains the name, then the attributes directly after it. What am I doing wrong?
Answer by 3dDude · Jul 29, 2010 at 02:07 PM
.ToString() returns the value of that value in string format. so if newD1 is not a string then it can't equal a string so your code should look like this:
var newD1String : String;
newD1String = newD1.ToString();
the problem with your code is that your telling it to make newD1 into a string but the only way that would work is if newD1 was a string already.
Thanks so much. But I have one more question that pertains to the same problem. See my Question above.