Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug code wont enter for/foreach loops

Discussion in 'Scripting' started by ItsMe1423, Apr 1, 2024.

  1. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    Code (CSharp):
    1.     public void AddLvl(GameObject button)
    2.     {
    3.         Debug.Log(((button.transform.parent).transform.parent).name);
    4.         Player currentPlayer = null;
    5.         for (int i = 0; i < Players.Count; i++)
    6.         {
    7.             Player player = Players[i];
    8.             Debug.Log("Player name: " + player.Name);
    9.             if (player.Name + " menu" == ((button.transform.parent).transform.parent).name)
    10.             {
    11.                 currentPlayer = player;
    12.                 Debug.Log("Set current player to: " + currentPlayer);
    13.             }
    14.         }
    15.         Debug.Log("Current player is: " + currentPlayer);
    16.         currentPlayer.Lvl += 1;
    17.         currentPlayer.Statpoints += 3;
    18.         UpdateStats(currentPlayer);
    19.         Debug.Log("Level added");
    20.         Debug.Log("player has " + currentPlayer.Statpoints);
    21.     }
    upload_2024-4-1_22-13-53.png
    This is the output
    Line 311 ----> line 16 in thread

    Help would be much appreciated, if more info is required feel free to ask
     
  2. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    2,047
    Im guessing the line 5 here is the 311 mentioned in the error?
     
  3. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    Line 311 ----> line 16 in thread it is said in the original post but all good
     
  4. Pandazole

    Pandazole

    Joined:
    Sep 23, 2019
    Posts:
    29
    It seems the players list is empty. Try to print the list count, if it's 0, you need to collect your players first.
     
    Bunny83 likes this.
  5. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    I have checked and it is empty but I am not sure why because I am adding a player to it every time the confirm button is pressed in this code
    Code (CSharp):
    1.     public void ConfirmPlayer(GameObject readyPlayerBox)
    2.     {
    3.         if (tempPlayer == lastMade)
    4.         {
    5.             return;
    6.         }
    7.         Vector3 scale = new Vector3(1f, 1f, 1f);
    8.         RectTransform originalRectTransform = readyPlayerBox.GetComponent<RectTransform>();
    9.  
    10.         Vector2 newBoxPos = new Vector2(originalRectTransform.anchoredPosition.x + Players.Count * 130 + 130, originalRectTransform.anchoredPosition.y);
    11.         GameObject playerBox = Instantiate(readyPlayerBox);
    12.         RectTransform newRectTransform = playerBox.GetComponent<RectTransform>();
    13.         TextMeshProUGUI playerText = playerBox.GetComponentInChildren<TextMeshProUGUI>();
    14.         playerText.rectTransform.SetParent(playerBox.transform.GetChild(0).parent);
    15.         playerBox.transform.SetParent(readyPlayerBox.transform.parent);
    16.  
    17.         playerBox.transform.name = tempPlayer.Name + " player button";
    18.         newRectTransform.localScale = scale;
    19.         playerText.enableWordWrapping = false;
    20.         playerText.name = tempPlayer.Name + " Text";
    21.         playerText.text = tempPlayer.Name;
    22.         playerText.rectTransform.anchoredPosition = new Vector2(0 , 0);
    23.         playerText.rectTransform.offsetMax = new Vector2(-5, -5);
    24.         playerText.rectTransform.offsetMin = new Vector2(5, 5);
    25.         newRectTransform.anchoredPosition = newBoxPos;
    26.         Players.Add(tempPlayer);
    27.         Debug.Log(Players.Count);
    28.         lastMade = Players.Last();
    which is ran before it is even possible to run the code in the original post

    EDIT: this is not the entire code just the chunk of it that matters
     
  6. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    2,047
    well.. one obvious answer is that there is no guarantee you find a player matching your criteria, as a result you exit the loop and its still null.
     
  7. Chubzdoomer

    Chubzdoomer

    Joined:
    Sep 27, 2014
    Posts:
    135
    Yep, I suspect something wonky is going on with this line of code:

    if (player.Name + " menu" == ((button.transform.parent).transform.parent).name)


    I would debug this hideous part of the condition above, as what it's returning could be different than what you expect:

    ((button.transform.parent).transform.parent).name)


    It probably wouldn't hurt to debug the player name portion as well (the part that appends "menu" to the end of the player's name; is the player's name what it should be?).
     
  8. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,124
    I don't quite follow your debugging logic here. So you have checked and the list is empty. So that explains the issue you had presented in this thread. You think the list shouldn't be empty, so that's the next thing you should look for. Are you sure that method you've shown that is adding players to that list is actually executed? If the answer is yes, are you sure that this is the "same" Players list? Common errors are executing methods on prefabs instead of the proper instances in the scene. This happens when you setup references to the wrong objects.

    You can use the second argument in the Debug.Log call and provide a "context" object that is linked with the log message. When you click on the log message in the console, Unity will highlight this context object. This can help figuring out on what objects which methods are actually executed. We can't really help you since we don't have your project. We don't even know if all the code you've shown is in the same script, where that script might be attached to and if there's additional code which may actually remove players or cliears the Players list at some point. Debugging is the most important skill a developer must master. When you encounter a bug it's important to understand it in order to solve it. Or like Ryan Hipple would say:
    .
     
    PraetorBlue and mopthrow like this.
  9. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,567
    That is just issues prone to happen all over and over again.

    But first:
    button.transform.parent.parent.name
    gives you the same name.

    But this makes the hierarchy rigid, any reparenting will break this code, same for renaming. Assign an Inspector reference or use GetComponent(s)InParent.
     
    Ryiah and Bunny83 like this.
  10. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    Firstly thank you all for the replies
    Secondly, I am unable to add a reference because this is a dynamic object, it's being added while the game is running this is why I have to use this, unless you have another way if you do I would love it hear it
     
  11. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    I have already debugged that it returns what I want it to return
     
  12. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    I have checked everything, it is the same list, it adds it, it doesn't remove it anywhere and pretty much everything you can think of. For some reason the list goes empty once it goes into the AddLvl function have you ever encountered something like that? what was the problem?

    Thank you for replying btw
     
  13. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    2,047
    OK,so this feels a bit more long shot but
    Code (CSharp):
    1.  Players.Add(tempPlayer);
    tempPlayer is not a local instance variable but a class wide by the looks of it, therefore you could ad 1000 players and they'd all be the same most likely.. the last one..

    Can you show the code? all of it? for that class, Im assuming confirmplayer is in the same class yes?
     
  14. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    I can put the code here but its kind of long and messy so get ready lol
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6. using System.Linq;
    7.  
    8. public class PlayerManager : MonoBehaviour
    9. {
    10.     [SerializeField] private Canvas canvas;
    11.     private Player lastMade, tempPlayer;
    12.     public GameObject playerMenu;
    13.     public TextMeshProUGUI strText, dexText, consText, chaText, wisText, arcmText;
    14.     public string characterName, tempName;
    15.     public List<Player> Players = new List<Player> {};
    16.     public int statsPerLevel = 3;
    17.     TextMeshProUGUI playerStrText = null, playerDexText = null, playerConsText = null, playerChaText = null, playerWisText = null, playerArcmText = null, Level = null, StatPoints = null;
    18.  
    19.     public class Player
    20.     {
    21.         private int str, wis, cons, cha, arcm, dex, lvl, statpoints;
    22.         private string name;
    23.         public int Str { get { return str; } set { str = value; } }
    24.         public int Wis { get { return wis; } set { wis = value; } }
    25.         public int Cons { get { return cons; } set { cons = value; } }
    26.         public int Cha { get { return cha; } set { cha = value; } }
    27.         public int Arcm { get { return arcm; } set { arcm = value; } }
    28.         public int Dex { get { return dex; } set { dex = value; } }
    29.         public int Lvl { get { return lvl; } set { lvl = value; } }
    30.         public int Statpoints { get { return statpoints; } set { statpoints = value; } }
    31.         public string Name { get { return name; } set { name = value; } }
    32.  
    33.  
    34.         public Player(string name, int str, int wis, int cons, int cha, int arcm, int dex, int lvl, int statpoints)
    35.         {
    36.             this.name = name;
    37.             this.str = str;
    38.             this.wis = wis;
    39.             this.cons = cons;
    40.             this.arcm = arcm;
    41.             this.dex = dex;
    42.             this.lvl = lvl;
    43.             this.statpoints = statpoints;
    44.         }
    45.  
    46.  
    47.     }
    48.     public void UpdateName(string name) { tempName = name; }
    49.  
    50.     public Player crateCharacter(string name)
    51.     {
    52.         bool are30 = false;
    53.         int statsum = 0;
    54.         int[] stats = new int[6];
    55.         while (are30 == false)
    56.         {
    57.             for (int i = 0; i < stats.Length; i++)
    58.             {
    59.                 stats[i] = Random.Range(1, 30);
    60.             }
    61.             for (int i = 0; i < stats.Length; i++)
    62.             {
    63.                 statsum = statsum + stats[i];
    64.             }
    65.             if (statsum == 30)
    66.             {
    67.                 are30 = true;
    68.             }
    69.             statsum = 0;
    70.         }
    71.         int str = stats[0], wis = stats[1], cons = stats[2], cha = stats[3], arcm = stats[4], dex = stats[5];
    72.         Player player = new(name, str, wis, cons, cha, arcm, dex, 0, 0);
    73.         player.Cha = stats[3];
    74.         return player;
    75.     }
    76.     public void SetPlayer()
    77.     {
    78.         Player player = null;
    79.         foreach (Player player1 in Players)
    80.         {
    81.             if (player1.Name == tempName)
    82.             {
    83.                 Debug.Log("Name exists.");
    84.                 return;
    85.             }
    86.         }
    87.         player = crateCharacter(tempName);
    88.         Debug.Log(player.Name);
    89.         tempPlayer = player;
    90.         strText.text = player.Str.ToString();
    91.         dexText.text = player.Dex.ToString();
    92.         consText.text = player.Cons.ToString();
    93.         chaText.text = player.Cha.ToString();
    94.         wisText.text = player.Wis.ToString();
    95.         arcmText.text = player.Arcm.ToString();
    96.         Debug.Log("Temp player is: "+tempPlayer.Name);
    97.         Debug.Log("List length in SetPlayer is: " + Players.Count);
    98.     }
    99.  
    100.     public void ConfirmPlayer(GameObject readyPlayerBox)
    101.     {
    102.         Debug.Log("Temp player in ConfirmPlayer is: " + tempPlayer.Name);
    103.         if (tempPlayer == lastMade)
    104.         {
    105.             return;
    106.         }
    107.         Vector3 scale = new Vector3(1f, 1f, 1f);
    108.         RectTransform originalRectTransform = readyPlayerBox.GetComponent<RectTransform>();
    109.  
    110.         Vector2 newBoxPos = new Vector2(originalRectTransform.anchoredPosition.x + Players.Count * 130 + 130, originalRectTransform.anchoredPosition.y);
    111.         GameObject playerBox = Instantiate(readyPlayerBox);
    112.         RectTransform newRectTransform = playerBox.GetComponent<RectTransform>();
    113.         TextMeshProUGUI playerText = playerBox.GetComponentInChildren<TextMeshProUGUI>();
    114.         playerText.rectTransform.SetParent(playerBox.transform.GetChild(0).parent);
    115.         playerBox.transform.SetParent(readyPlayerBox.transform.parent);
    116.  
    117.         playerBox.transform.name = tempPlayer.Name + " player button";
    118.         newRectTransform.localScale = scale;
    119.         playerText.enableWordWrapping = false;
    120.         playerText.name = tempPlayer.Name + " Text";
    121.         playerText.text = tempPlayer.Name;
    122.         playerText.rectTransform.anchoredPosition = new Vector2(0 , 0);
    123.         playerText.rectTransform.offsetMax = new Vector2(-5, -5);
    124.         playerText.rectTransform.offsetMin = new Vector2(5, 5);
    125.         newRectTransform.anchoredPosition = newBoxPos;
    126.         Players.Add(tempPlayer);
    127.         Debug.Log(Players.Count);
    128.         lastMade = Players.Last();
    129.         Debug.Log("List length in ConfirmPlayer after setting lastmade is: " + Players.Count);
    130.         //Specific player menu making
    131.         GameObject MakeSpecificMenu = Instantiate(playerMenu);
    132.         RectTransform rt = MakeSpecificMenu.GetComponent<RectTransform>();
    133.         MakeSpecificMenu.transform.SetParent(playerMenu.transform.parent);
    134.         MakeSpecificMenu.name = tempPlayer.Name + " menu";
    135.         rt.position = new Vector2(500, 50);
    136.         rt.localScale = new Vector3(1, 1, 1);
    137.         //List of stat texts
    138.         TextMeshProUGUI[] stats = MakeSpecificMenu.GetComponentsInChildren<TextMeshProUGUI>();
    139.         foreach (var stat in stats)
    140.         {
    141.             if (stat.name == "LevelNum")
    142.             {
    143.                 stat.name = tempPlayer.Name + " LevelNum";
    144.                 stat.text = tempPlayer.Lvl.ToString();
    145.             }
    146.             else if (stat.name == "StatPointsNum")
    147.             {
    148.                 stat.name = tempPlayer.Name + " StatPointsNum";
    149.                 stat.text = tempPlayer.Statpoints.ToString();
    150.             }
    151.             else if (stat.name == "StrenghNum")
    152.             {
    153.                 stat.name = tempPlayer.Name + " StrenghNum";
    154.                 stat.text = tempPlayer.Str.ToString();
    155.             }
    156.             else if (stat.name == "DexterityNum")
    157.             {
    158.                 stat.name = tempPlayer.Name + " DexterityNum";
    159.                 stat.text = tempPlayer.Dex.ToString();
    160.             }
    161.             else if (stat.name == "ConstitutionNum")
    162.             {
    163.                 stat.name = tempPlayer.Name + " ConstitutionNum";
    164.                 stat.text = tempPlayer.Cons.ToString();
    165.             }
    166.             else if (stat.name == "CharismaNum")
    167.             {
    168.                 stat.name = tempPlayer.Name + " CharismaNum";
    169.                 stat.text = tempPlayer.Cha.ToString();
    170.             }
    171.             else if (stat.name == "WisdomNum")
    172.             {
    173.                 stat.name = tempPlayer.Name + " WisdomNum";
    174.                 stat.text = tempPlayer.Wis.ToString();
    175.             }
    176.             else if (stat.name == "ArcmanaNum")
    177.             {
    178.                 stat.name = tempPlayer.Name + " ArcmanaNum";
    179.                 stat.text = tempPlayer.Arcm.ToString();
    180.             }
    181.         }
    182.     }
    183.  
    184.  
    185.     public void OpenSpecificMenu(GameObject button)
    186.     {
    187.         PlayerManager currentMenu = null;
    188.         PlayerManager[] menus = canvas.GetComponentsInChildren<PlayerManager>();
    189.         string playerName = button.GetComponentInChildren<TextMeshProUGUI>().text;
    190.         Debug.Log("List length in OpenSpecificMenu is: " + Players.Count);
    191.  
    192.         foreach (var item in menus)
    193.         {
    194.             Debug.Log(item.name);
    195.         }
    196.  
    197.         foreach (var item in menus)
    198.         {
    199.             Debug.Log(item.name);
    200.             if (item.name == playerName)
    201.             {
    202.                 currentMenu = item;
    203.             }
    204.         }
    205.         Debug.Log("Current menu is: " + currentMenu);
    206.         currentMenu.SetActive(true);
    207.  
    208.  
    209.     }
    210.  
    211.     public void UpdateStats(Player currentPlayer)
    212.     {
    213.         playerStrText.text = currentPlayer.Str.ToString();
    214.         playerDexText.text = currentPlayer.Dex.ToString();
    215.         playerConsText.text = currentPlayer.Cons.ToString();
    216.         playerChaText.text = currentPlayer.Cha.ToString();
    217.         playerWisText.text = currentPlayer.Wis.ToString();
    218.         playerArcmText.text = currentPlayer.Arcm.ToString();
    219.         Level.text = currentPlayer.Lvl.ToString();
    220.         StatPoints.text = currentPlayer.Statpoints.ToString();
    221.     }
    222.     //Start of buttons
    223.  
    224.     //Levels
    225.     public void AddLvl(GameObject button)
    226.     {
    227.         Debug.Log(button.transform.parent.parent.name);
    228.         Player currentPlayer = null;
    229.         Debug.Log(Players.Count, button);
    230.         for (int i = 0; i < Players.Count; i++)
    231.         {
    232.             Player player = Players[i];
    233.             Debug.Log("Player name: " + player.Name);
    234.             if (player.Name + " menu" == button.transform.parent.parent.name)
    235.             {
    236.                 currentPlayer = player;
    237.                 Debug.Log("Set current player to: " + currentPlayer);
    238.             }
    239.         }
    240.         Debug.Log("Current player is: " + currentPlayer);
    241.         currentPlayer.Lvl += 1;
    242.         currentPlayer.Statpoints += 3;
    243.         UpdateStats(currentPlayer);
    244.         Debug.Log("Level added");
    245.         Debug.Log("player has " + currentPlayer.Statpoints);
    246.     }
    247.     public void RemoveLvl(GameObject button)
    248.     {
    249.         Player currentPlayer = null;
    250.         foreach (var player in Players)
    251.         {
    252.             if (player.Name == (button.transform.parent).name)
    253.             {
    254.                 currentPlayer = player;
    255.             }
    256.         }
    257.         if (currentPlayer.Lvl <= 0)
    258.             return;
    259.         currentPlayer.Lvl -= 1;
    260.         currentPlayer.Statpoints -= 3;
    261.         UpdateStats(currentPlayer);
    262.         Debug.Log("Level removed");
    263.     }
    264.     //Strengh
    265.     public void AddStr(GameObject button)
    266.     {
    267.         Player currentPlayer = null;
    268.         foreach (var player in Players)
    269.         {
    270.             if (player.Name == (button.transform.parent).name)
    271.             {
    272.                 currentPlayer = player;
    273.             }
    274.         }
    275.         if (currentPlayer.Statpoints <= 0) { return; }
    276.         currentPlayer.Str += 1;
    277.         currentPlayer.Statpoints -= 1;
    278.         Debug.Log("Strengh added");
    279.         UpdateStats(currentPlayer);
    280.     }
    281.     public void RemoveStr(GameObject button)
    282.     {
    283.         Player currentPlayer = null;
    284.         foreach (var player in Players)
    285.         {
    286.             if (player.Name == (button.transform.parent).name)
    287.             {
    288.                 currentPlayer = player;
    289.             }
    290.         }
    291.         if (currentPlayer.Str <= 0)
    292.             return;
    293.         currentPlayer.Str -= 1;
    294.         currentPlayer.Statpoints += 1;
    295.         Debug.Log("Strengh removed");
    296.         UpdateStats(currentPlayer);
    297.     }
    298.     //Dexterity
    299.     public void AddDex(GameObject button)
    300.     {
    301.         Player currentPlayer = null;
    302.         foreach (var player in Players)
    303.         {
    304.             if (player.Name == (button.transform.parent).name)
    305.             {
    306.                 currentPlayer = player;
    307.             }
    308.         }
    309.         if (currentPlayer.Statpoints <= 0) { return; }
    310.         currentPlayer.Dex += 1;
    311.         currentPlayer.Statpoints -= 1;
    312.         Debug.Log("Dexterity added");
    313.         UpdateStats(currentPlayer);
    314.     }
    315.     public void RemoveDex(GameObject button)
    316.     {
    317.         Player currentPlayer = null;
    318.         foreach (var player in Players)
    319.         {
    320.             if (player.Name == (button.transform.parent).name)
    321.             {
    322.                 currentPlayer = player;
    323.             }
    324.         }
    325.         if (currentPlayer.Dex <= 0)
    326.             return;
    327.         currentPlayer.Dex -= 1;
    328.         currentPlayer.Statpoints += 1;
    329.         Debug.Log("Dexterity removed");
    330.         UpdateStats(currentPlayer);
    331.     }
    332.     //Constitution
    333.     public void AddCons(GameObject button)
    334.     {
    335.         Player currentPlayer = null;
    336.         foreach (var player in Players)
    337.         {
    338.             if (player.Name == (button.transform.parent).name)
    339.             {
    340.                 currentPlayer = player;
    341.             }
    342.         }
    343.         if (currentPlayer.Statpoints <= 0) { return; }
    344.         currentPlayer.Cons += 1;
    345.         currentPlayer.Statpoints -= 1;
    346.         Debug.Log("Constitution added");
    347.         UpdateStats(currentPlayer);
    348.     }
    349.     public void RemoveCons(GameObject button)
    350.     {
    351.     Player currentPlayer = null;
    352.     foreach (var player in Players)
    353.     {
    354.         if (player.Name == (button.transform.parent).name)
    355.         {
    356.             currentPlayer = player;
    357.         }
    358.     }
    359.     if (currentPlayer.Cons <= 0)
    360.             return;
    361.         currentPlayer.Cons -= 1;
    362.         currentPlayer.Statpoints += 1;
    363.         Debug.Log("Constitution removed");
    364.         UpdateStats(currentPlayer);
    365.     }
    366.     //Charisma
    367.     public void AddCha(GameObject button)
    368.     {
    369.         Player currentPlayer = null;
    370.         foreach (var player in Players)
    371.         {
    372.             if (player.Name == (button.transform.parent).name)
    373.             {
    374.             currentPlayer = player;
    375.             }
    376.         }
    377.         if (currentPlayer.Statpoints <= 0) { return; }
    378.         currentPlayer.Cha += 1;
    379.         currentPlayer.Statpoints -= 1;
    380.         Debug.Log("Charisma added");
    381.         UpdateStats(currentPlayer);
    382.     }
    383.     public void RemoveCha(GameObject button)
    384.     {
    385.     Player currentPlayer = null;
    386.     foreach (var player in Players)
    387.     {
    388.         if (player.Name == (button.transform.parent).name)
    389.         {
    390.             currentPlayer = player;
    391.         }
    392.     }
    393.     if (currentPlayer.Cha <= 0)
    394.             return;
    395.         currentPlayer.Cha -= 1;
    396.         currentPlayer.Statpoints += 1;
    397.         Debug.Log("Charisma removed");
    398.         UpdateStats(currentPlayer);
    399.     }
    400.     //Wisdom
    401.     public void AddWis(GameObject button)
    402.     {
    403.     Player currentPlayer = null;
    404.         foreach (var player in Players)
    405.         {
    406.             if (player.Name == (button.transform.parent).name)
    407.             {
    408.                 currentPlayer = player;
    409.             }
    410.         }
    411.         if (currentPlayer.Statpoints <= 0) { return; }
    412.         currentPlayer.Wis += 1;
    413.         currentPlayer.Statpoints -= 1;
    414.         Debug.Log("Wisdom added");
    415.         UpdateStats(currentPlayer);
    416.     }
    417.     public void RemoveWis(GameObject button)
    418.     {
    419.             Player currentPlayer = null;
    420.         foreach (var player in Players)
    421.         {
    422.             if (player.Name == (button.transform.parent).name)
    423.             {
    424.                 currentPlayer = player;
    425.             }
    426.         }
    427.         if (currentPlayer.Wis <= 0)
    428.             return;
    429.         currentPlayer.Wis -= 1;
    430.         currentPlayer.Statpoints += 1;
    431.         Debug.Log("Wisdom removed");
    432.         UpdateStats(currentPlayer);
    433.     }
    434.     //Arcmana
    435.     public void AddArcm(GameObject button)
    436.     {
    437.             Player currentPlayer = null;
    438.         foreach (var player in Players)
    439.         {
    440.             if (player.Name == (button.transform.parent).name)
    441.             {
    442.                 currentPlayer = player;
    443.             }
    444.         }
    445.         if (currentPlayer.Statpoints <= 0) { return; }
    446.         currentPlayer.Arcm += 1;
    447.         currentPlayer.Statpoints -= 1;
    448.         Debug.Log("Arcmana added");
    449.         UpdateStats(currentPlayer);
    450.     }
    451.     public void RemoveArcm(GameObject button)
    452.     {
    453.             Player currentPlayer = null;
    454.         foreach (var player in Players)
    455.         {
    456.             if (player.Name == (button.transform.parent).name)
    457.             {
    458.                 currentPlayer = player;
    459.             }
    460.         }
    461.         if (currentPlayer.Arcm <= 0)
    462.             return;
    463.         currentPlayer.Arcm -= 1;
    464.         currentPlayer.Statpoints += 1;
    465.         Debug.Log("Arcmana removed");
    466.         Debug.Log("Player has arcmana: " + currentPlayer.Arcm);
    467.         UpdateStats(currentPlayer);
    468.     }
    469.     /// </End of button
    470.  
    471.     public void FixedUpdate()
    472.     {
    473.         //playerStrText.text = currentPlayer.Str.ToString();
    474.         //playerDexText.text = currentPlayer.Dex.ToString();
    475.         //playerConsText.text = currentPlayer.Cons.ToString();
    476.         //playerChaText.text = currentPlayer.Cha.ToString();
    477.         //playerWisText.text = currentPlayer.Wis.ToString();
    478.         //playerArcmText.text = currentPlayer.Arcm.ToString();
    479.         //Level.text = currentPlayer.Lvl.ToString();
    480.         //StatPoints.text = currentPlayer.Statpoints.ToString();
    481.     }
    482. }
     
  15. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    2,047
    OK

    so looking at your original code (and the scope of the full code - btw, im a strong believer in function first, pretty second)
    so, it seems ConfirmPlayer must happen before you can call the others, this is not apparently the case in the debug shown.. therefore players.count would be zero.. as none of the debug messages from confirmplayer were in the log.

    As others have mentioned it also could be 2 copies of this code. Its not like this is a singleton etc
     
  16. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    That's good to hear lol would have been bad if you were the other way around
    And about the debug I have added the debug messages in the ConfirmPlayer after I posted this so they are not in the original pic I sent
    and as far as the player list being empty it is physically impossible I will explain why in the following photo
    upload_2024-4-2_18-2-40.png
    Log after plus button being pressed:
    upload_2024-4-2_18-3-27.png
    and therefore it's impossible to press the button while the list is empty

    EDIT: The first menu is open by clicking the big Plus button near the player button
     
    Last edited: Apr 2, 2024
  17. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    2,047
    well if the list is empty you have a second copy of the class somewhere

    Add a Start and debug GetInstanceID() and name

    that way you can find out where the instances are
     
  18. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    I have only two scripts on this project so far - PlayerManager and DragDrop. DragDrop takes care of exactly what it sound like, dragging objects. All of the other stuff is being taken care off in PlayerManager

    What does GetInstanceID() do?
     
  19. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    The instance id does change what does that mean?
     
  20. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    2,047
    it means you have multiple copies dear
    you sure the script is not on things you're spawning?
     
  21. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    It is... I hadn't realised this was a problem until now, I am now looking for a different way to do this because so far I have been using the gameObject to set the player menu
    I will try to make a MenuManager script that will be in charge of opening the correct menu and more menu-related stuff I will do in the future but I need to figure out how to use variables from other scripts, now specifically the Players list.
    Do you know how do I do it?
     
  22. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    2,047
    well, this is the fun part of the design.. you're supposed to work it out, as you know your app way better than we do
    you could just reference the original copy... one way or another..
     
  23. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,515
    You can have the object responsible for adding it at runtime be responsible for assigning the references too.
     
  24. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,124
    How have you checked it? You haven't really applied my advice of adding a context object to your log statements. Specifically inside SetPlayer replace this:
    Code (CSharp):
    1. Debug.Log("Temp player is: "+tempPlayer.Name);
    with this
    Code (CSharp):
    1. Debug.Log("Temp player is: "+tempPlayer.Name, this);
    As I said in my post:
    Do you have a prefab of your PlayerManager gameobject? A common mistake is that you've setup your UI callbacks wrongly. You may have referenced the prefab instead of the instance in the scene. Note that a prefab is itself an instance which does not live in the scene but in the project. You can actually call methods on those prefab objects. However those are separate instances. When you actually instantiate a prefab (either through dragging the prefab into the scene or by using Instantiate at runtime) you will create a clone / copy in the scene. Only instances in the scene will get Unity's magic callbacks like Start and Update. Prefab objects do not receive any of those.

    Anyways, add a context object to all relevant log messages and click on the messages to see which objects they came from. I'm sure you will see that they origin from different objects. Once you know which message belongs to which object, you can trace back who or from where the execution was initiated. You probably have some wrong references.
     
  25. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,079
    Dynamic or static, you absolutely DO NOT EVER need to play "Where's GameWaldo" with all this crazy kinda stuff:

    Learn the "adaptor" pattern as I like to call it: you make an adaptor that has all the stuff necessary to make one of your "dynamic objects" and you use that adaptor to find all the stuff you need within one of these "objects," whether it is levels you can play, powerups you can activate, etc.

    This really is The Unity Way(tm) when it comes to prefabs, especially dynamic and UI prefabs. Our entire company uses this pattern at scale, and we NEVER do any GetComponent<T>() or FindWhatever() noise.

    See attached example. It really works. Look through it until you understand what every single part does. Every single part contributes to the solution which does NOT use Get or Find anywhere.
     

    Attached Files:

    Last edited: Apr 3, 2024
    bugfinders likes this.
  26. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    It now works, I have moved the script to a different empty object dedicated to it, I would still like to learn that adaptor pattern but I cant seem to understand how to open that file do you care to explain?
     
  27. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    2,047
    its a unity package, open unity with a new project, right click project area, click import package
     
    Kurt-Dekker likes this.
  28. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    yeah but after that just understand what the code is supposed to do? cause the actual scene is empty
     
  29. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,079
    Then you didn't open the scene. Open it.

    If you do not have the Unity UGUI system installed, install it the same way you put it in your main project.