Unity Firebase example error: Cannot implicitly convert type `string' to `UnityEngine.Object'

I am getting an error on the code in this tutorial

https://firebase.google.com/docs/database/unity/save-data

Below is the code section I am having trouble with. I am getting the error on two lines I commented out below

result[“uid”] = uid: this gives me the "Cannot implicitly convert type string' to UnityEngine.Object’

result[“score”] = score: this gives me the "Cannot implicitly convert type int' to UnityEngine.Object’

public class LeaderboardEntry
{
public string uid;
public int score = 0;

public LeaderboardEntry()
{
}

public LeaderboardEntry(string uid, int score)
{
this.uid = uid;
this.score = score;
}

public Dictionary<string, Object> ToDictionary()
{
Dictionary<string, Object> result = new Dictionary<string, Object>();

//============GETTING ERROR HERE==========================
result[“uid”] = uid;
result[“score”] = score;
//=========================================================

return result;
}
}

Any help would be much appreciated, thanks!

Ah, I think I figured this out, I need explicitly write System.Object, since by default since I am inheriting from MonoBehaviour, it was using Unity’s Object

1 Like

Could you please elaborate on how you fixed this.

Thanks

Glad you figured it out. Next time please use code tags. It makes it much easier to read and help out.

1 Like

Can someone please help. I have the same issue but can’t get rid of the error. Here is my code…

public class FirebaseController : MonoBehaviour {

    [System.Serializable]
    public class LeaderBoardEntry : System.Object {
    public string uid;
    public int score = 0;

    public LeaderBoardEntry() {
    }

    public LeaderBoardEntry(string uid, int score) {
        this.uid = uid;
        this.score = score;
    }

    public Dictionary<string, Object> ToDictionary() {
        Dictionary<string, Object> result = new Dictionary<string, Object>();
        result["uid"] = uid; //Line:26
        result["score"] = score; //Line:27

        return result;
    }
    }

    DatabaseReference mDatabaseRef;

    // Use this for initialization
    void Start () {
        // Set up the Editor before calling into the realtime database.
    FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://downhillslalom-8c3e6.firebaseio.com/");

    // Get the root reference location of the database.
    mDatabaseRef = FirebaseDatabase.DefaultInstance.RootReference;
    }

    public void setHighscoreData(int Hscore) {
        string uid = PlayerPrefs.GetString("userName");
        int score = Hscore;

        string key = mDatabaseRef.Child("Scores").Push().Key;

        LeaderBoardEntry entry = new LeaderBoardEntry(uid, score);
        Dictionary<string, Object> entryValues = entry.ToDictionary();

        Dictionary<string, Object> childUpdates = new Dictionary<string, Object>();
        childUpdates["/Scores/" + key] = entryValues;

        mDatabaseRef.UpdateChildrenAsync(childUpdates);


    }
}

I get these errors:

(26,25): error CS0029: Cannot implicitly convert type string' to UnityEngine.Object’
(27,27): error CS0029: Cannot implicitly convert type int' to UnityEngine.Object’

Thanks

Replace Object with System.Object in the Dictionary code:

    public Dictionary<string, System.Object> ToDictionary()
    {
        Dictionary<string, System.Object> result = new Dictionary<string, System.Object>();
        result["uid"] = uid;
        result["score"] = score;

        return result;
    }

I could be wrong but object (lowercase o) may also work. The reason Object does not is because it thinks you are trying to use the Object class provided by unity as opposed to the built-in object type.