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

Cant understand how this wouldnt work

Discussion in 'Scripting' started by UnlitRyun, May 23, 2021.

  1. UnlitRyun

    UnlitRyun

    Joined:
    Feb 15, 2018
    Posts:
    10
    public void PersonalEventRecordRoom(string _roomName, GameObject _collidedPlayer)
    {
    if (pePlayer1 == null || _collidedPlayer) { pePlayer1 = _collidedPlayer; pePlayerRoomName1 = _roomName; }
    else if (pePlayer2 == null || _collidedPlayer) { pePlayer2 = _collidedPlayer; pePlayerRoomName2 = _roomName; }
    else if (pePlayer3 == null || _collidedPlayer) { pePlayer3 = _collidedPlayer; pePlayerRoomName3 = _roomName; }
    else if (pePlayer4 == null || _collidedPlayer) { pePlayer4 = _collidedPlayer; pePlayerRoomName4 = _roomName; }
    }

    This code above is being called from a trigger collider. It should run so it will record all 4 different people (multiplayer) will have a selected spot and when they enter a new collider with a different room name, it will just overweight the room name variable and then pePlayer variable will stay the same. I know what I just said doesn't make a lot of sense but its not working. None of the else if statement are being called. It works so if the pePlayer variable is null, then it will slot in the _collidedPlayer into it. If it is run again if pePlayer = collidered player, it will just overweight the room name. Its not working and I cant understand why. Thanks for any help. There is no errors. its just assigning pePlayer1 and pePlayerRoomName1 and not the later numbers.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Honestly, the above code is very very very hairy code, formatted or not.

    How to break down hairy lines of code:

    http://plbm.com/?p=248

    You have a series of conditions and testing and setting and oh my, it's all one giant blob.

    Break it up... respect social distancing in your code... one thing per line please.
     
  3. MDADigital

    MDADigital

    Joined:
    Apr 18, 2020
    Posts:
    2,198
    Put those players in an array please.
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,525
    Well I guess your "or" operators
    ||
    should be "and" operators
    &&
    . Otherwise your code wouldn't make much sense. Because if "_collidedPlayer" actually contains a valid reference, you would only ever enter the first if statement. I don't think that was the intention. Like MDADigital said, you probably should use an array for those 4 players. Also since your second condition is always the same, it should be refactored into a seperate if statement. You can do an early exit if the _collidedPlayer variable is not set.

    Though the overall point of the method isn't really clear. Assuming you meant to use && instead of || and your 4 "pePlayer" variables are null initially, you could only call your "PersonalEventRecordRoom" method 4 times. After that the method would not do anything since all of the pePlayer variables have been populated.

    Also note that method names should be verbs as methods generally do something. So the verb should represent what the method does. From your name I can't really make much sense of that method ^^.

    So the quickly fixed method would look something like that

    Code (CSharp):
    1. public void PersonalEventRecordRoom(string _roomName, GameObject _collidedPlayer)
    2. {
    3.     if (!_collidedPlayer)
    4.         return;
    5.     if (pePlayer1 == null)
    6.     {
    7.         pePlayer1 = _collidedPlayer;
    8.         pePlayerRoomName1 = _roomName;
    9.     }
    10.     else if (pePlayer2 == null )
    11.     {
    12.         pePlayer2 = _collidedPlayer;
    13.         pePlayerRoomName2 = _roomName;
    14.     }
    15.     else if (pePlayer3 == null)
    16.     {
    17.         pePlayer3 = _collidedPlayer;
    18.         pePlayerRoomName3 = _roomName;
    19.     }
    20.     else if (pePlayer4 == null)
    21.     {
    22.         pePlayer4 = _collidedPlayer;
    23.         pePlayerRoomName4 = _roomName;
    24.     }
    25. }
    Though with an array it would look like this:

    public struct PlayerWithRoomName
    {
    public GameObject player;
    public string roomName;
    }
    public PlayerWithRoomName[] players = new PlayerWithRoomName[4];

    Code (CSharp):
    1. public void PersonalEventRecordRoom(string _roomName, GameObject _collidedPlayer)
    2. {
    3.     if (!_collidedPlayer)
    4.         return;
    5.     for(int i = 0; i < players.Length; i++)
    6.     {
    7.         if (players[i].player == null)
    8.         {
    9.             players[i].player = _collidedPlayer;
    10.             players[i].roomName = _roomName;
    11.             return;
    12.         }
    13.     }
    14. }
    Though while this works with an array, it's probably better to use

    Code (CSharp):
    1.         if (players[i].player == null)
    2.         {
    3.             players[i] = new PlayerWithRoomName {
    4.                 player = _collidedPlayer,
    5.                 roomName = _roomName
    6.             };
    7.             return;
    8.         }
    9.  
    inside the for loop or use a class instead of a struct.
     
    Last edited: May 23, 2021