1

I am making an Android game, there is code to send highscore info to my web's PHP code.

Strange thing is, this works well in unity-editor, but after I make an .apk and run on real android phone, it does not work and gets errors.

Error is following (known by Eclipse logcat)

05-05 22:56:47.997: I/Unity(20561): NullReferenceException: Object reference not set to an instance of an object 05-05 22:56:47.997: I/Unity(20561): at HighScore.GetHash (System.String usedString) [0x00000] in :0 05-05 22:56:47.997: I/Unity(20561): at HighScore+c__Iterator14.MoveNext () [0x00000] in :0

Code is, function of registering nickname when firstly run highscore.

 public IEnumerator RegisterName(string name)
{ 
    string finalResult = "";
    string tables = "";
    for (int m = 0; m < difficultyModes.Length; m++)
    {//Create a list of tables to send 
        if (m < difficultyModes.Length - 1)
        {
            tables += difficultyModes[m].databaseName + " ";
        }
        else
        {
            tables += difficultyModes[m].databaseName;
        }
    } 
    WWWForm rsFm = new WWWForm();
    rsFm.AddField("name", name);
    rsFm.AddField("tables", tables);
    rsFm.AddField("hash", GetHash(name));
    WWW rs = new WWW(registerUserURL, rsFm);
    yield return rs;
    Debug.Log(rs.text + " : " + difficultyModes[modeIndex].displayName);
    finalResult = rs.text;
    if (finalResult.Equals("Already Used"))
    {
        runningHsServer = 0;
        status = "Name Already Used";
        TitleScript.Instance.PopupMsgWithNoBack("Name is already taken.", true);
        yield break;
    }
    else if (finalResult.Equals("Registration Complete"))
    {//We Registered Now Update Score
        PlayerPrefs.SetInt("nameRegistered", 1);
        PlayerPrefs.SetString("registeredName", name);
        TitleScript.Instance.PopupMsgWithNoBack("Name registered!", true);
        TitleScript.Instance.PopupNo();
        NewUI.Instance.myNick.text = name;
        myNickName = name;
        NewUI.Instance.OpenHigh();
    }
    else
    {
        runningHsServer = 0;
        status = finalResult; yield break;
    }

}

and GetHash part. (at here, NullReference error occurs)

string GetHash(string usedString)
{ //Create a Hash to send to server
    MD5 md5 = MD5.Create();
    //byte[] bytes = Encoding.ASCII.GetBytes(usedString+secretKey);     // this wrong because can't receive korean character
    byte[] bytes = Encoding.UTF8.GetBytes(usedString + secretKey);
    byte[] hash = md5.ComputeHash(bytes);

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < hash.Length; i++)
    {
        sb.Append(hash[i].ToString("x2"));
    }
    return sb.ToString();
}
1
  • Have you tried adding null-checks to the GetHash method?
    – Max Yankov
    Commented May 5, 2015 at 15:55

2 Answers 2

5

Use MD5CryptoServiceProvider instead of MD5.Create()

var md5 = new MD5CryptoServiceProvider();

MD5.Create() doesn't return an object on Unity Android when the Stripping Level is set to Micro mscorlib, but 'new MD5CryptoServiceProvider()' does.

0

So this is because unity build setting's Stripping Level setting.

Originally I set most strippied setting (Use micro mscorlib) for more smaller .apk file size, but this blocked some code functions.

So I set it back to Disabled, above function works fine at android phone too.

Not the answer you're looking for? Browse other questions tagged or ask your own question.