Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Access PlayerInput in child component

Discussion in 'Scripting' started by Xstyler, Oct 6, 2020.

  1. Xstyler

    Xstyler

    Joined:
    Nov 5, 2015
    Posts:
    9
    I'm trying to use a new Input System for local multiplayer.

    upload_2020-10-6_22-8-18.png

    upload_2020-10-6_22-6-17.png

    If the script is on the same GameObject as PlayerInput, I can easily access methods OnJump, OnPickup like this:

    Code (CSharp):
    1. private void OnJump(InputValue value)
    2. {
    3.      jump = value.isPressed;
    4. }

    How can I access these methods in the child component?

    upload_2020-10-6_22-11-40.png

    I want to use OnPickup method:
    Code (CSharp):
    1. private void OnPickup(InputValue value)
    2. {
    3.      pickedUp = value.isPressed;
    4. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Best practice is to ALWAYS drag a reference of the child script into a public variable of the correct type.

    If you want to live more dangerously, you can use this API:

    https://docs.unity3d.com/ScriptReference/Component.GetComponentInChildren.html

    BUT! It can fail if: you have more than 1 script instance in the children, there is 0 script instances in the children, etc.

    So always best to directly drag a reference into the script that needs it. That is The Unity Way(tm).
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    You could change that dropdown on your PlayerInput from "Send Messages" to "Broadcast Messages". That will also send messages to child objects.

    You could also look into the "Send Unity Events" mode. If you select that, you'll get a list of events underneath that dropdown and you can assign them specifically to call specific functions on specific components just like you can do for UI buttons.
     
    Xstyler likes this.
  4. Xstyler

    Xstyler

    Joined:
    Nov 5, 2015
    Posts:
    9
    @Kurt-Dekker I was trying with reference but I have no idea how to do that.

    @PraetorBlue "Broadcast Messages" works, but there is a lot of children GameObjects, I assume there could be a performance issue. I don't know how "Send Unity Events" works but I will try to google it. Thank you for the direction.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    You make a public variable of the type you need. In the case of what you're doing I'm not 100% sure it applies now that Praetor chimed in, but it is basically the "drag this GameObject with this script into this slot" mechanism that 99.99% of every unity tutorial ever made uses.
     
  6. Xstyler

    Xstyler

    Joined:
    Nov 5, 2015
    Posts:
    9
    yes, like I did for PickableHolder. The problem is I don't know how to write code that would expose OnJump, OnPickup...

    I created Event callbacks as PraetorBlue suggested.
    upload_2020-10-7_19-40-24.png

    I have two problems I don't understand.

    Code runs 3 times, and I would a lot of questions about this without answers.
    I fix it with the if.

    The second problem is the counter. In console counter updates normally but in the inspector, it stays at 0.
    I don't know why?

    Code (CSharp):
    1.  
    2. public int counter = 0;
    3. public void OnPickup(InputAction.CallbackContext context)
    4. {
    5.         if(context.performed && gameObject.scene.IsValid()) {
    6.                 counter++;
    7.                 print("Picked");
    8.         }
    9. }