• 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 /
avatar image
0
Question by sgdotnetcoder · Feb 06, 2020 at 02:02 AM · button trigger eventsavatarscene-changeface model

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);
         }




alt text

alt text

,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);
         }



page1.jpg (35.8 kB)
page2.jpg (68.9 kB)
Comment
Add comment · Show 1
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
avatar image Mym2o · Feb 06, 2020 at 10:19 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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();
  }
Comment
Add comment · Show 1 · Share
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
avatar image sgdotnetcoder · Feb 07, 2020 at 01:03 AM 0
Share

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.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

122 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

Related Questions

Function triggered when scene changes? 4 Answers

UI canvas gets copied when changing scene 2 Answers

Switching Scene Immediately 2 Answers

Why won't invoke work in my script to delay the scene change? 1 Answer

How to prevent Unity3D making unnecessary changes after saving scene with no actual changes 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges