What am i doing wrong here? (file.exists)
Hi, im trying to tell if a file exists. I know the file is there, but it is just returing that its not. What have i done wrong? Here is the full code.
using UnityEngine;
using System.IO;
public class handleData : MonoBehaviour {
public static string datapath;
public static string correct;
void Start()
{
datapath = Application.dataPath;
}
public static string HandleLogin(string username, string password)
{
if (File.Exists(datapath + "/Resources/" + username + ".xml"))
{
correct = "ok";
}
else
{
correct = "fail";
}
return correct;
}
}
@Galleria Your code seems to correct. Just make sure you attached the script to gameobject.
Answer by JVince · Jun 07, 2016 at 11:00 AM
I saw your file is under Resources, so why don't you use Resource.Load for this check? https://docs.unity3d.com/ScriptReference/Resources.Load.html
if ((Resources.Load("xmlfileName") as TextAsset) != null)
correct = "ok";
else
correct = "fail";
And I think you should also check the username parameter. Hope this help.
i have tried this and now im getting the following error
"Load can only be called from the main thread."
call HandleLogin(param1, param2) function at Start(). Ref: http://forum.unity3d.com/threads/load-can-only-be-called-from-main-thread-help.315675/ :)
Answer by Zodiarc · Jun 07, 2016 at 10:57 AM
Is there a reason why those properties have to be static?
Try to change datapath to handleData.datapath and correct to handleData.corrent
And also: Why waste space for storing a binary result ('ok' and 'fail') in a string instead of a boolean (true, false)?
Your answer
Follow this Question
Related Questions
New Project Gibberish Folder 0 Answers
Find file in unkown location (Steam) 0 Answers
How to display .tif images via script? 1 Answer
move file inside streamingAssets during build 0 Answers
Syntax 501 error, How to upload without error to FTP Server 1 Answer