- Home /
Button clicked from script but nothing happens .,Button triggered but nothing happens on the screen
Hi, this is my third day with unity coding but not new to c#.
i downloaded a sample code from online and try to modify it. but when i move the button click event to "Void start" . nothing happens after the event is triggered when clicked event was fired from the script. it should generate a 3D face model. but if i run the same code and trigger the click event from the UI. everything works fine.
the reason why i use timer to trigger the click event is because the start method is loading the licenses.so i wait till the license is loaded then i run the generator.
i also tried triggering the click event method "GenerateRandomAvatar" directly from the timer directly.code finished running but nothing happens also. any help will be really appreciated .
i also attached the picture before and after from click event.
Button startButton;
protected virtual void Start()
{
var ui = controls.Select(b => b.gameObject).ToArray();
if (!SampleUtils.CheckIfSupported(progressText, ui, sdkType))
return;
// first of all, initialize the SDK
if (!AvatarSdkMgr.IsInitialized)
AvatarSdkMgr.Init(sdkType: sdkType);
StartCoroutine(Initialize());
// Anti-aliasing is required for hair shader, otherwise nice transparent texture won't work.
// Another option is to use cutout shader, but the look with this shader isn't that great.
#if UNITY_STANDALONE_WIN || UNITY_EDITOR || UNITY_EDITOR
QualitySettings.antiAliasing = 8;
#else
QualitySettings.antiAliasing = 4;
#endif
foreach (var b in controls)
{
if (b is Button)
{
if(b.name.Contains("Random")){
startButton = (Button)b;
//get button from the UI
}
}
}
startTimer();//trigger the button click after 10 second after screen initilise
}
Timer timer;
public void startTimer() {
timer=new Timer();
timer.Interval = 1000 * 10;
timer.Elapsed += OnTimer;
timer.Start();
}
public void OnTimer(object obj,ElapsedEventArgs args) {
timer.Stop();
startButton.onClick.Invoke();
}
//button click
public virtual void GenerateRandomAvatar()
{
string photoPath = @"myphoto.jpg";
byte[] bytes = File.ReadAllBytes(photoPath);
// StartCoroutine(GenerateAvatarFunc(testPhoto.bytes));
StartCoroutine(GenerateAvatarFunc(bytes));
}
protected virtual IEnumerator GenerateAvatarFunc(byte[] photoBytes)
{
var avatarObject = GameObject.Find("ItSeez3D Avatar");
Destroy(avatarObject);
SetControlsInteractable(false);
photoPreview.gameObject.SetActive(false);
yield return StartCoroutine(GenerateAndDisplayHead(photoBytes, pipelineType));
SetControlsInteractable(true);
if (generateHaircutButton != null)
generateHaircutButton.gameObject.SetActive(pipelineType == PipelineType.FACE);
}
,I am extreme new to unity(third days i think?) ,i downloaded an online source and modify myself but i am not new to C#.
i have a button on the screen. if i manually click on the button and trigger the button event then everything will work fine. So i decide to move the code to program "Start" method or manually click the button from the "Start" function .But the screen refuse to change to new scene. below is my code
Button startButton;
protected virtual void Start()
{
var ui = controls.Select(b => b.gameObject).ToArray();
if (!SampleUtils.CheckIfSupported(progressText, ui, sdkType))
return;
// first of all, initialize the SDK
if (!AvatarSdkMgr.IsInitialized)
AvatarSdkMgr.Init(sdkType: sdkType);
StartCoroutine(Initialize());
// Anti-aliasing is required for hair shader, otherwise nice transparent texture won't work.
// Another option is to use cutout shader, but the look with this shader isn't that great.
#if UNITY_STANDALONE_WIN || UNITY_EDITOR || UNITY_EDITOR
QualitySettings.antiAliasing = 8;
#else
QualitySettings.antiAliasing = 4;
#endif
foreach (var b in controls)
{
if (b is Button)
{
if(b.name.Contains("Random")){
startButton = (Button)b;
//get button from the UI
}
}
}
public void startTimer() {
timer=new Timer();
timer.Interval = 1000 * 10;
timer.Elapsed += OnTimer;
timer.Start();
}
public void OnTimer(object obj,ElapsedEventArgs args) {
timer.Stop();
startButton.onClick.Invoke();
}
//button click
public virtual void GenerateRandomAvatar()
{
string photoPath = @"myphoto.jpg";
byte[] bytes = File.ReadAllBytes(photoPath);
// StartCoroutine(GenerateAvatarFunc(testPhoto.bytes));
StartCoroutine(GenerateAvatarFunc(bytes));
}
protected virtual IEnumerator GenerateAvatarFunc(byte[] photoBytes)
{
var avatarObject = GameObject.Find("ItSeez3D Avatar");
Destroy(avatarObject);
SetControlsInteractable(false);
photoPreview.gameObject.SetActive(false);
yield return StartCoroutine(GenerateAndDisplayHead(photoBytes, pipelineType));
SetControlsInteractable(true);
if (generateHaircutButton != null)
generateHaircutButton.gameObject.SetActive(pipelineType == PipelineType.FACE);
}
From your code I understand that you need only the "Random Photo" button, is it right? Try to remove the foreach statement where you initialize startButton variable and change its declaration to
public Button startButton;
After that, drag and drop the button from the hierarchy panel to the field that it will appear in the inspector of game object which contains this script and try in this way
Answer by WARdd · Feb 06, 2020 at 11:15 AM
It sounds like the code in that OnTimer function is not being called at all. Try adding a log message to double check:
public void OnTimer(object obj,ElapsedEventArgs args) {
timer.Stop();
Debug.Log("Invoking button!");
startButton.onClick.Invoke();
}
If it's not being called you could just use subroutines to call the function after a delay, something like this (I'm writing the code by heart so you'll have to fix it probably):
private void Start {
StartCoroutine(GenerateDelayed());
}
private IEnumerator GenerateDelayed() {
yield return new WaitForSeconds(10f);
GenerateAvatar();
}
Hi mate, thanks for replying.the timer function was called during "Void Start" with the "StartTimer" function and i saw the code being hit with line by line debug. is just that after the code finished running,nothing happens.
however, your "GenerateDelayed" works perfectly. Thank you so much.