- Home /
Cannot convert int to String
methas script :
var meth : int;
function LoadData()
{
var path : String = "C:/Program Files (x86)/moldbsaves/" + methas;
if(File.Exists(path + "/savegame.mbs"))
{
var loadeddata = File.OpenText(path + "/savegame.mbs");
var readdata = loadeddata.ReadLine();
while(readdata != null)
{
var getdata = readdata.ToString().Split("=" [0]);
if(getdata[0] == "meth")methas.meth = getdata[1]; //here i got error.
readdata = loadeddata.ReadLine();
}
loadeddata.Close();
}
}
how i can fix it ?
Seems like you are trying to store a String in an int container.
i tried this but i have another script with many functions where meth is int,i can't just change it to string.
"i have another script with many functions where meth is int".
ints and strings are very different things - I can't think of any situation where it wouldn't be obvious which is the correct datatype to use. Just make sure you're consistent.
Answer by WillNode · Dec 21, 2014 at 03:44 PM
try this :
if(getdata[0] == "meth")methas.meth = parseInt(getdata[1]);
the parseInt()
function will convert String
to int
in javascript. but if you want to convert String
to int
in CSharp, use integer.Parse()
no errors but when i click load game nothing happened..(
have you check that this line code can be accessed by unity at runtime? try this code again :
if(getdata[0] == "meth"){
methas.meth = parseInt(getdata[1]);
print(getdata[1]);
}
if this code not appear in unity console, then there is something wrong in your code.
Answer by Seneral · Dec 21, 2014 at 03:29 PM
http://docs.go-mono.com/?link=M%3aSystem.Int32.Parse(System.String)
if(getdata[0] == "meth")methas.meth = Int32.Parse(getdata[1]);
@GH0STfdd:
Use int.Parse
.
"int" is actually an alias for "System.Int32". Since in Unity you usually don't have a using System;
at the top (since it conflicts with some classnames of Unity classes) you would have to type the full class name including the namespace. Since "int" exists there's no need to ever use System.Int32 explicitly