- Home /
new UI tools problem
i want to run this code with new ui tool, this is my code
using UnityEngine;
using System.Collections;
public class FecebookLog : MonoBehaviour {
void Awake()
{
FB.Init(SetInit, OnHideUnity);
}
/*public void ChangeToScene(string SceneLoad)
{
Application.LoadLevel(SceneLoad);
}*/
private void SetInit()
{
Debug.Log ("Init Done");
if(FB.IsLoggedIn)
{
Debug.Log("FB Login ");
} else {
FBlogin();
}
}
private void OnHideUnity(bool isGameShown)
{
if(!isGameShown)
{
Time.timeScale = 0;
}
else
{
Time.timeScale = 1;
}
}
void FBlogin()
{
FB.Login("user_about_me , user_birthday", AuthCallback);
}
void AuthCallback(FBResult result)
{
if(FB.IsLoggedIn)
{
Debug.Log("FB Login Work!!");
Application.LoadLevel("Menu");
}
else
{
Debug.Log("FB Login Failed");
Application.LoadLevel("FB");
}
}
// Use this for initialization
}
I created a new UI button (FB LOGIN) and I linked this code with this button, but the problem that this code on automatically when I enter in play mode, I want that this code works only I click on my button
Help me please Thank you
Answer by Mmmpies · Feb 04, 2015 at 11:30 AM
If you don't want it running until being clicked remove the Awake Function.
Both Awake and Start functions run when play is first clicked and Update runs every frame but your other functions will only get called by external events.
EDIT
OK Remove was the wrong word, rename that to something like
public void IveBeenClicked()
Instead of Awake then copy that script onto the new UI button, click + in the OnClick part of the button in the inspector, a slot appears so drag your button (with this script on it) onto that slot. Next to that slot there's a dropdown so select your script name -> IveBeenClicked.
Then when you click that button it'll call that function, I'm assuming that's what you want anyway.
Thank you for your answer :) @$$anonymous$$mmpies and if I delete Awake, where I move this code snippet
FB.Init(SetInit, OnHideUnity);
Answer by SuperMasterBlasterLaser · Feb 04, 2015 at 03:49 PM
You starting your FBlogin when your GameObject is AWAKE. You need to make your SetInit public and do not call it from Awake. In your editor on your Button UI, there is on click events. Attach your GameObject that contains FecebookLog script there and call your SetInit function.
Answer by AndresBarrera · Feb 04, 2015 at 03:51 PM
Are you using the OnClick event of the button?? Look at this video, around 10:30
Your answer
Follow this Question
Related Questions
New UI Button Dynamic onClick Listener problem 1 Answer
Script variable doesn't appear in UI's inspector 2 Answers
How to create a 4.6 UI button NOT from prefab NOT in unity editor only from C# 1 Answer
Is there a better way to access the single active Toggle in a ToggleGroup? 4 Answers
UI button dosent show function 1 Answer