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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Assigning current user to a variable

Discussion in 'Input System' started by PaRsOnIsPhErE, Feb 16, 2020.

  1. PaRsOnIsPhErE

    PaRsOnIsPhErE

    Joined:
    Dec 19, 2018
    Posts:
    19
    Hi all,

    I've been trying to work this out on and off other the last couple of weeks. I'm sure it's super easy, and i'm over thinking the whole thing.

    All I need is too be able to acquire what #User number the current playerinput is an interger... as I will then change player sprite based on what user / player number they are, just using a switch statement.

    I have looked into the below but to no avail.

    Code (CSharp):
    1. InputUser user {get;}
    2. PlayerInput user {get;}
    3. Playerindex {get;}
    Could anyone point in the right direction?? How do I correctly use the above properties??

    Thank you all.
     
  2. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    PlayerInput.playerIndex seems to be what you're looking for? What about it didn't work for you?

    If the problem is that playerIndex will stay fixed when another player leaves -- meaning that if, for example, playerIndex #2 leaves, playerIndex #3 does not become playerIndex #2 but instead playerIndex #2 will become vacant --, you can derive the strictly sequential index from the player's slot in PlayerInput.all.

    Code (CSharp):
    1. var index = PlayerInput.all.IndexOfReference(myPlayer);
     
  3. PaRsOnIsPhErE

    PaRsOnIsPhErE

    Joined:
    Dec 19, 2018
    Posts:
    19
    @Rene-Damm, thank you! Finally got it to work! It was something so simple, but I was just over thinking the whole thing, thinking I needed something more complex for it to work. I wasn't attempting to access playerIndex from the PlayerInput component... just trying to access it from... well, basically nowhere... the moment I seen you had PlayerInput.playerIndex, I face palmed... felt so embarrassed... I couldn't see it written like that in the documentation anywhere so I just completely drew a blank for some reason, thank you again for your help!

    I think it's great that you spend time out of your day to help and respond to us developers on here, amazing work! Keep it up! I will buy you a beer if we ever meet!