Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Hansel41 · May 14, 2021 at 02:32 PM · databaseauthentication

So far, I've created an authentication system using firebase but database doesn't work as it should

How to save firebase Realtime database in currently signed in users ID ? I'm able to post and retrieve data into firebase but it creates new users every time i put data into fields. Please share some code snippet on how to save and retrieve data into current Firebaseuser using c#. It'd mean a lot to me.

Here is the code that I'm using: using System.Collections; using System.Collections.Generic; using UnityEngine; using Firebase; using Firebase.Database; //using Firebase.Unity.Editor; using UnityEngine.UI; using Firebase.Extensions; using Firebase.Firestore; using System; using Firebase.Auth;

public class User {

     public string score;
     public string name;
     public string email;
     public string bio;
     public string timeStamp;
    
    
     public User ()
     {
     }
 
     public User (string score, string name, string email, string bio, string timeStamp)
     {
             this.name = name;
             this.email = email;
             this.bio = bio;
             this.timeStamp = timeStamp;
             this.score = score;
     }

}

public class RealtimeDatabase : MonoBehaviour { DatabaseReference reference; DatabaseReference reference2; public InputField nameInput; public InputField emailInput; public InputField bioInput; public Text msg; string docEmail; public GameObject prefab; public Transform userBarParent; public Text eventListener; string eventText; ///

/// /// public Text scoreText;

     void Start ()
     {
             //FirebaseApp.DefaultInstance.SetEditorDatabaseUrl (Constant.FirebaseDatabaseURL);
             reference = FirebaseDatabase.DefaultInstance.RootReference;
             reference2 = FirebaseDatabase.DefaultInstance.GetReference ("users");
 
             reference2.ChildAdded += HandleChildAdded;
             reference2.ChildChanged += HandleChildChanged;
             reference2.ChildRemoved += HandleChildRemoved;
             reference2.ChildMoved += HandleChildMoved;
     Score.cashValue += 350;
     }
 
 public void AddNewData()
 {
         msg.text = "Adding Data...";
         User user = new User(scoreText.text, nameInput.text, emailInput.text, bioInput.text, DateTime.Now.ToString());
      
         string json = JsonUtility.ToJson(user);
         reference.Child("users").Child(user.email).SetRawJsonValueAsync(json);
         Debug.Log("RTDB Updated");
         msg.text = "Data Added !";
         eventText = "New Child Added";
 }
   
 public void ClearData ()
     {
 
     foreach (Transform child in userBarParent.transform) {
         Destroy (child.gameObject);
     }
 }
 
 
 
     public void GetUsersData ()
     {
             FirebaseDatabase db = FirebaseDatabase.DefaultInstance;
             db.GetReference ("users").OrderByChild("timeStamp").LimitToFirst (10).GetValueAsync ().ContinueWithOnMainThread (task => {
                     if (task.IsFaulted) {
                            
                     } else if (task.IsCompleted) {
                             DataSnapshot snapshot = task.Result;
                             foreach (DataSnapshot user in snapshot.Children) {
                                     IDictionary dictUser = (IDictionary)user.Value;
                                     Debug.Log ( dictUser ["bio"]);
 
                                     GameObject userBar = Instantiate (prefab, transform.position, transform.rotation);
                                     userBar.transform.SetParent (userBarParent, false);
                                     userBar.transform.GetChild (0).GetComponent<Text> ().text = "Name: "+dictUser ["name"];
                                     userBar.transform.GetChild (1).GetComponent<Text> ().text = "Bio: "+dictUser ["bio"];
                                     userBar.transform.GetChild(2).GetComponent<Text>().text = "Score" + dictUser["score"];
                             }
                     }
             });
     }
 
     void HandleChildAdded (object sender, ChildChangedEventArgs args)
     {
             if (args.DatabaseError != null) {
                     Debug.LogError (args.DatabaseError.Message);
                     return;
             }
             // This is fired when new child is added
             Debug.Log ("Child Added");
             eventListener.text = eventText;
     }
 
     void HandleChildChanged (object sender, ChildChangedEventArgs args)
     {
             if (args.DatabaseError != null) {
                     Debug.LogError (args.DatabaseError.Message);
                     return;
             }
             // This is fired when child gets updated
 
             Debug.Log ("Child Changed");
     }
 
     void HandleChildRemoved (object sender, ChildChangedEventArgs args)
     {
             if (args.DatabaseError != null) {
                     Debug.LogError (args.DatabaseError.Message);
                     return;
             }
             // This is fired when child gets removed
 
             Debug.Log ("Child Removed");
     }
 
     void HandleChildMoved (object sender, ChildChangedEventArgs args)
     {
             if (args.DatabaseError != null) {
                     Debug.LogError (args.DatabaseError.Message);
                     return;
             }
             // This is fired when child reorder (order-by query)
 
             Debug.Log ("Child Moved");
     }
 
 
     //Remove Listner OnDestroy
 
 private void OnDestroy ()
 {
             reference2.ChildAdded -= HandleChildAdded;
             reference2.ChildChanged -= HandleChildChanged;
             reference2.ChildRemoved -= HandleChildRemoved;
             reference2.ChildMoved -= HandleChildMoved;
     }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

165 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Where to clear Firebase local cache in Unity Editor 1 Answer

Inventory that loads data from an XML 0 Answers

how to define a user id securely 0 Answers

Get Other Players information (those who are installed) ? 0 Answers

How to setup AWS dynamoDB that I can CRUD (create, read, update, delete) data by Unity? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges