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

Local Co-op // How to Reference the new 'Joined Player' Prefab?

Discussion in 'Input System' started by SuperPox, Oct 11, 2020.

  1. SuperPox

    SuperPox

    Joined:
    Apr 29, 2020
    Posts:
    12
    Hi guys,

    I'm trying to understand the "Player Input Manager" component and want to start with the simplest first step.

    I've made an empty game object, added the "Player Input Manager" component to it, and under the "Joining" heading selected "Join when Button Pressed" and slotted in my player prefab. When I connect a second controller I can control both players between my two controllers.

    The problem is I don't know how to reference either the Unity Event "Join when Button Pressed" or the player prefab I've slotted in. I've looked for tutorials or Unity documentation on this issue and can't find much of anything.

    If someone would be so kind, can you tell me how to reference my second player?
     
  2. SuperPox

    SuperPox

    Joined:
    Apr 29, 2020
    Posts:
    12
    If others find my problem through search here is how I solved it
     

    Attached Files:

  3. MaverickVII

    MaverickVII

    Joined:
    Apr 26, 2019
    Posts:
    1
    did you ever find a solution?
     
  4. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    All enabled
    PlayerInput
    instances are globally accessible via
    PlayerInput.all
    .

    Code (CSharp):
    1. // Loop through players.
    2. foreach (var player in PlayerInput.all)
    3.     Debug.Log($"Player {player.playerIndex} is {player}");
    On join, there is also a notification on
    PlayerInputManager
    when a player joins. Can be set up from the inspector or from code.

    Code (CSharp):
    1. // Get notified when a player joins.
    2. PlayerInputManager.instance.onPlayerJoined +=
    3.     player =>
    4.     {
    5.         Debug.Log($"Player {player.playerIndex} joined");
    6.     };
    (same for
    onPlayerLeft
    )