Not sure if this is a bug or that I am doing something wrong, but when I spawn a player trough: PlayerInput.Instantiate(GameObject, Int32, String, Int32, InputDevice)
I get the following error messages:
Does anybody know why?
The code for this:
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Users;
public class PlayerManagerInputHandeler : MonoBehaviour
{
//*Target objects
//Player prefab
public GameObject playerPrefab;
//*Components
/// <summary>The InputUser struct of the PlayerInputManager</summary>
//(This is used to listen to inputs and filter them based on player/controller paring)
private InputUser user0;
//*Memory
/// <summary>The memory value of the start button</summary>
private float buttonMem = 0;
private int playerInputCountMem = 0;
//#### Unity functions ####
//## Awake is called when this script instance is being loaded
private void Awake()
{
//Get the PlayerInputManager's InputUser (user 0 is the first created user)
user0 = PlayerInput.all[0].user;
//Unpair and repair all unpaired devices from input manager/user 0 (basically refresh it)
user0.UnpairDevices();
foreach (InputDevice unPairDev in InputUser.GetUnpairedInputDevices())
{
InputUser.PerformPairingWithDevice(unPairDev, user0);
}
}
//## Update is called once per frame
private void Update()
{
//Get any newly added devices
if (InputUser.GetUnpairedInputDevices().Count != 0)
{
//Unpair and repair all unpaired devices from input manager/user 0 (basically refresh it)
user0.UnpairDevices();
foreach (InputDevice unPairDev in InputUser.GetUnpairedInputDevices())
{
InputUser.PerformPairingWithDevice(unPairDev, user0);
}
}
}
//#### My functions ####
//## Inputs
/// <summary>Reads a device's start input and creates a new player to pair with that device</summary>
public void StartDevice(InputAction.CallbackContext context)
{
if (buttonMem != context.ReadValue<float>())
{
buttonMem = context.ReadValue<float>();
if (context.ReadValue<float>() > 0)
{
buttonMem = 0;
//Spawn a new player and assign the device from the received input
CreatePlayer(playerPrefab, context.control.device);
}
}
}
//## Outputs
/// <summary>Spawn a player and assign a device to it</summary>
private void CreatePlayer(GameObject playerPrefab, InputDevice inputDevice)
{
//Create an player instance and get it's InputUser
var playerInstance = PlayerInput.Instantiate(playerPrefab, PlayerInput.all.Count, default, default, inputDevice);
var userInstance = playerInstance.user;
if (inputDevice == Mouse.current || inputDevice == Keyboard.current)
{
//Pair mouse and keyboard to the created player
InputUser.PerformPairingWithDevice(Mouse.current, userInstance);
InputUser.PerformPairingWithDevice(Keyboard.current, userInstance);
//Unpair mouse and keyboard
user0.UnpairDevice(Mouse.current);
user0.UnpairDevice(Keyboard.current);
}
else
{
//Pair a device to the created player
InputUser.PerformPairingWithDevice(inputDevice, userInstance);
//Unpair current device
user0.UnpairDevice(inputDevice);
}
}
}