- Home /
Reset Input.GetJoystickNames() array
hi folks,
do you know if there is any way to reset the joystick array which is raised by one each time you plug in a new gamepad?
for example, if i connect 4 gamepads, the array looks like this:
[0] gamepad1
[1] gamepad2
[2] gamepad3
[3] gamepad4
if i now disconnect the first 2 gamepads it looks like this:
[0] empty
[1] empty
[2] gamepad3
[3] gamepad4
i know, this behaviour is on purpose so that the player slots are not switched on a disconnect, but what i want is that the gaps will be closed, gamepad3 becomes player1 and so on..
[0] gamepad3
[1] gamepad4
the only workaround i know is to close and reopen the editor or build.
do you know any better solution?
Answer by 72daniel72 · Jan 26, 2017 at 09:18 AM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharPlacer : MonoBehaviour
{
public GameObject[] chars;
public Transform[] startPosses;
// Use this for initialization
void Start()
{
int playerSetback = 0;
for (int i = 0; i < Input.GetJoystickNames().Length; i++)
{
if (Input.GetJoystickNames()[i] == "")
{
playerSetback++;
}
else //character creation
{
int realI = i - playerSetback;
GameObject curr = Instantiate(chars[0], startPosses[realI].position, Quaternion.identity);
curr.GetComponent<MovePlayer>().id = (realI + 1);
curr.GetComponent<MovePlayer>().prefix = "P" + (realI + 1).ToString() + "_";
}
}
}
// Update is called once per frame
void Update()
{
}
}
What i do is "ignore" the empty spaces. By counting a variable, i can fake that these empty spaces don't exist! @anszwa
thank you for your answer daniel,
unfortunatly this is exactly the way i solved it until now but i run into some troubles with it.
for example:
i want to test my project by using a lot of different gamepads, so each time i plug in a new gamepad i have to close and restart the editor/build to get the new one into the Player 1 slot.
do you know if there is any way to simulate the "editor initialization process" so that the input array is created new?
@LazyDog_Rich sorry for the very late reply...
No, unfortunately I endet working on this project a couple of days after my request.
Your answer
Follow this Question
Related Questions
how can I assign and use different controller schemes for a single keyboard? 0 Answers
Check if mouse is down while in Edit Mode 1 Answer
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
Is there a way to temporarily disable an inputmanager axis through script? 1 Answer
Touch inputs on a Desktop type device 2 Answers