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

Calling Event One Time with continuous action

Discussion in 'Editor & General Support' started by ClaudiaKrog, Aug 28, 2020.

  1. ClaudiaKrog

    ClaudiaKrog

    Joined:
    Sep 30, 2017
    Posts:
    47
    I currently have around 15 different bools in the Update function to set off different actions. This is not ideal, so I am trying to change this to events that will be called for each of the 15 cases.

    Specifically, I need a different event to be called when a gameobject is selected by the user. However, this gameobject will be held onto, so I would imagine the event would be called over and over, when I only want the event to be called one time, when the object is first picked up.

    I could create a sort of trigger bool, to allow the event to be called one time, but then why would I not just keep all my bools in the update function?

    Any advice is appreciated!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
  3. ClaudiaKrog

    ClaudiaKrog

    Joined:
    Sep 30, 2017
    Posts:
    47
    Would the actual C# event be different from the Unity Event? Wouldn't both events be called continuously, if they are triggered by a continuous action?
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    They're similar.

    Well.. maybe? What kind of "continuous action" are we talking about? In your op you said something about a user selecting an object. That doesn't sound continuous.
     
  5. ClaudiaKrog

    ClaudiaKrog

    Joined:
    Sep 30, 2017
    Posts:
    47
    This is a VR game - So I would call the event when the object becomes held. I suppose I could try and call the event when the object has been held for 1 frame, and then perhaps the event will be called once, at the start of the action. I don't know if that is the best way
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    How are you detecting that an object is being held? Im not familiar with the vr frameworks but running some code when an object is picked up seems like a really use case. Im sure there's a way to do it.
     
  7. ClaudiaKrog

    ClaudiaKrog

    Joined:
    Sep 30, 2017
    Posts:
    47
    I'm seeing if the object has been grabbed, which calls while held I believe:

    if (mainStars[0] != null)
    {
    //star 1 is grabbed
    if (mainStarGrab[0].grabbedBy != null)
    {
    //Call PauseEvent
    }
    }

    There must be a one time call for when it is picked up, but I am not sure! I will search for it.
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    Figure out where that
    grabbedBy
    variable is assigned. That will be where you can fire off your one-time event.
     
  9. ClaudiaKrog

    ClaudiaKrog

    Joined:
    Sep 30, 2017
    Posts:
    47
    Thanks, I will try it. I may be not understanding events very well. I would think that because the isGrabbed bool will stay true while it is held, the event will be fired off more than once.

    Edit: I found a function in the OVRGrabbable script when the object is grabbed, and tested that it calls only one time, while object is held.

    Thanks for the help!
     
  10. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    ClaudiaKrog likes this.