Firebase에서 유저 프로필 업데이트가 안됩니다.

조회수 2380회

https://firebase.google.com/docs/auth/unity/manage-users?hl=ko

이 가이드를 보고 따라만들고 있는데요

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Firebase.Auth;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Threading.Tasks;


public class CreatUser : MonoBehaviour {

    private FirebaseAuth auth;
    public InputField Email, Pass, UserName;
    public Button SignUpButton, SignInButton, UpdateButton, CheckButton;
    public Text ErrorText;
    public string userName;
//  public Firebase.Auth.UserProfile profile;


    // Use this for initialization
    void Start () {
        auth = FirebaseAuth.DefaultInstance;
        SignUpButton.onClick.AddListener(() => SignUp(Email.text, Pass.text));
        SignInButton.onClick.AddListener(() => SingIn(Email.text, Pass.text));
        UpdateButton.onClick.AddListener (() => SetUser (UserName.text));
        CheckButton.onClick.AddListener (() => GetUser ());
        DontDestroyOnLoad(this);
    }

    // Update is called once per frame
    void Update () {

    }

    public void SignUp(string email, string password){
        if (string.IsNullOrEmpty (email) || string.IsNullOrEmpty (password)) {
            Debug.Log ("Email or Password is Null");
        } 
        else {
            auth.CreateUserWithEmailAndPasswordAsync (email, password).ContinueWith (task => {
                if (task.IsCanceled) {
                    Debug.LogError ("CreateUserWithEmailAndPasswordAsync was canceled.");
                    return;
                }
                if (task.IsFaulted) {
                    Debug.LogError ("CreateUserWithEmailAndPasswordAsync error: " + task.Exception);
                    if (task.Exception.InnerExceptions.Count > 0)
                        UpdateErrorMessage (task.Exception.InnerExceptions [0].Message);
                    return;
                }

                FirebaseUser newUser = task.Result; // Firebase user has been created.

                newUser.SendEmailVerificationAsync().ContinueWith(t =>
                    {
                    Debug.Log("SendEmailVerification");
                    UpdateErrorMessage("SendEmailVerificationAsync success");
                    });
                Debug.Log("Email Send");
                Debug.LogFormat ("Firebase User Created Successfully:{0}({1})",
                    newUser.DisplayName, newUser.UserId);
                UpdateErrorMessage ("Sign Up Success");
            });
        }
    }

    private void UpdateErrorMessage(string message){
        ErrorText.text = message;
        Invoke ("ClearErrorMessage", 3);
    }

    void ClearErrorMessage(){
        ErrorText.text = "";
    }

    public void SingIn(string email, string password){
        UpdateErrorMessage ("Sign In Push");
        auth.SignInWithEmailAndPasswordAsync (email, password).ContinueWith (task => {
            if (task.IsCanceled) {
                Debug.LogError ("SingInWithEmailAndPasswordAsync Canceled.");
                return;
            }
            if (task.IsFaulted) {
                Debug.LogError ("SingInWithEmailAndPasswordAsync error : " + task.Exception);
                if (task.Exception.InnerExceptions.Count > 0)
                    UpdateErrorMessage (task.Exception.InnerExceptions [0].Message);
                return;
            }

            FirebaseUser user = task.Result;
            if(!user.IsEmailVerified){
                UpdateErrorMessage("The email is not verified yet.");
                auth.SignOut();
                return;
            }
            UpdateErrorMessage ("User Signed In Successfully : " + user.UserId + user.ProviderId);
            //Debug.LogFormat ("User Singed In Successfully : {0} ({1)}", //로그인 성공
            //  user.DisplayName, user.UserId);

        });

    }

    private void SetUser(string name){
        Firebase.Auth.FirebaseUser userFP = auth.CurrentUser;
        if (userFP != null) {
            Firebase.Auth.UserProfile profile;
            profile.DisplayName = name;
            profile.PhotoUrl = "";
            userFP.UpdateUserProfileAsync (profile).ContinueWith (task => {
                UpdateErrorMessage ("task");
                if (task.IsCompleted) {
                    UpdateErrorMessage ("User Profile Updated");
                }
            });
        }
    }

    public void GetUser(){
        Firebase.Auth.FirebaseUser userFP = auth.CurrentUser;
        userName = userFP.DisplayName;
        UpdateErrorMessage ("username : " + userName);
//      SceneManager.LoadScene("Example");
    }
}


사용자 프로필 업데이트 하는 부분을 SetUser 함수로 만들었습니다.

그런데 자꾸

Assets/Script/CreatUser.cs(108,4): error CS0165: Use of unassigned local variable `profile'

이 에러가 떠서 실행이 안됩니다.. 가이드 대로 했는데 에러가 뜨니까 어떻게 해야할지를 모르겠습니다.

에러를 검색해보니 저랑 같은 에러가 뜬 다른경우의 사람들은 초기화를 안해줘서 그렇다며 해당 변수를 null로 초기화 하고 해보라고 되어있더라구요.

그래서 저도 profile = null; 을 추가 해 봤는데 에러는 사라졌지만 함수가 동작을 하지 않습니다... 이럴 땐 어떻게 해야하나요ㅠㅠ??

  • (•́ ✖ •̀)
    알 수 없는 사용자

1 답변

  •         Firebase.Auth.UserProfile profile;
            profile.DisplayName = name;
            profile.PhotoUrl = "";
    

    이 부분을

            Firebase.Auth.UserProfile profile=new Firebase.Auth.UserProfile();
            profile.DisplayName = name;
            profile.PhotoUrl = "";
    

    이렇게 바꿔보세요. `profile'이 할당되지 않은 채로 멤버변수에 접근을 해서 나는 문제입니다.

    그런데 profile = null;을 추가하더라도 동일한 오류가 나야 정상인데 오류가 안났다는 것은 아예 private void SetUser() 함수 호출이 안된것 같아 보여요.

    • (•́ ✖ •̀)
      알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)