Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

problem when clicking on Button

Discussion in 'Scripting' started by Trild123787898, Feb 3, 2020.

  1. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    I can’t understand why it doesn’t want to work, if I click on the Button, it should work the script inside FixedUpdate, but for some reason it doesn’t want to do it, if I press the key then everything works, I use event tigger (pointer down, up), what is the problem?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.EventSystems;
    6.  
    7. public class TestScript : MonoBehaviour
    8. {
    9.     public ConfigurableJoint confdet5;
    10.     private bool click;
    11.  
    12.     void Start()
    13.     {
    14.        
    15.         confdet5 = gameObject.GetComponent<ConfigurableJoint>();
    16.  
    17.  
    18.  
    19.     }
    20.     public void TaskOn()
    21.     {
    22.         click = true;
    23.     }
    24.     public void TaskOn2()
    25.     {
    26.         click = false;
    27.     }
    28.  
    29.     void FixedUpdate()
    30.     {
    31.         if (click == true)
    32.         {
    33.             confdet5.angularYMotion = ConfigurableJointMotion.Free;
    34.         }
    35.         if (click == false)
    36.         {
    37.             confdet5.angularYMotion = ConfigurableJointMotion.Locked;
    38.         }
    39.     }
    40. }
    41.  
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    I'm confused as to why you are handling the change in FixedUpdate. Why not just handle it when you click on the button?

    What is TaskOn and TaskOn2 hooked up to?

    Have you added debug messages to see if the code is even being triggered?

    If it's a UI button, why not just hook it to the OnClick? If it's not a UI button, what kind of button is it?

    If it's the Pointer system as you describe, how do you have it set up in your inspector?
     
  3. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    TaskOn is responsible for pressing the button (event when it is pressed), TaskOn2 is responsible when the button is released,