Search Unity

public int numcountblah is set to 20, but in game it only goes up to 6?

Discussion in 'Scripting' started by mvfsullivan, Feb 20, 2018.

  1. mvfsullivan

    mvfsullivan

    Joined:
    Feb 20, 2018
    Posts:
    1
    For some reason when I set a specific public int to anything higher than 6, the increment stops at 6 and keeps going.

    It is important to note that later on, I will be adding a toggle that the user can enable that increases the spawn limit, that is why a public int treelimit represents the number, so that I can assign it to a toggle and have the number change.

    What I have is a game where characters can collect resources and spawn object with said resources. I have a current limit of 5 on the object spawned (a bush), but when I set the int to say, 20, in the game the items spawn and the counter panel (I have a popup for each time the player places an item, telling them how many they've placed and how many they have left), shows 1/20, 2/20 properly, up until 6/20, where it freezes at 6, and the items can continue to be spawned indefinitely (well passed the limit represented by the int.

    Could someone please explain to me why this is happening??

    Here is some code:

    Code (CSharp):
    1. if (this.VibItem == "tree" && this.Blocktree.GetComponent<StayNoneTrigger>().GoStay)
    2.     {
    3.         this.treelimit = 20;
    4.         this.treeplaced = 0;
    5.         for (int m = 0; m < this.Mytree.Length; m++)
    6.         {
    7.             if (this.Mytree[m] != null)
    8.             {
    9.                 this.treeplaced++;
    10.             }
    11.         }
    12.         if (this.treeplaced < this.treelimit)
    13.         {
    14.             if (PlayerPrefs.GetInt("GoGame") == 0)
    15.             {
    16.                 gameObject = PhotonNetwork.Instantiate(this.BlocktreePrefab.name, this.Blocktree.transform.position, this.Blocktree.transform.rotation, 0);
    17.             }
    18.             if (PlayerPrefs.GetInt("GoGame") == 1)
    19.             {
    20.                 gameObject = UnityEngine.Object.Instantiate<GameObject>(this.BlocktreePrefab, this.Blocktree.transform.position, this.Blocktree.transform.rotation);
    21.             }
    22.             gameObject.GetComponent<RotateAddPoint>()._ControllerResourse = base.gameObject.GetComponent<ControllerResourse>();
    23.             gameObject.GetComponent<RotateAddPoint>().TypeTrr = this.Typetree;
    24.             this.stone -= 10;
    25.             this.wood -= 50;
    26.             for (int n = 0; n < this.Mytree.Length; n++)
    27.             {
    28.                 if (this.Mytree[n] == null)
    29.                 {
    30.                     this.Mytree[n] = gameObject;
    31.                     n = this.Mytree.Length;
    32.                 }
    33.             }
    34.             this.treeplaced++;
    35.         }
    36.         this.TextCol.text = "Mill: " + this.treeplaced + "/" + this.treelimit;
    37.         this.PanelInformItem.GetComponent<DisablePanel>().tm = 1.5f;
    38.         this.PanelInformItem.SetActive(true);
    39.     }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I am curious as why you didn't just use a List instead of an array as you could just compare to the Count value instead of doing a loop and then you could just add instead of looking for your first null slot in another loop.

    As for your issue, usually when something like that happens, I see an error with it. Are you getting any errors?
     
  3. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Is Mytree.Length set to 5 or 6? That could be why treeplaced is getting stuck, you only increment it within the first for loop and then once more in the second branch, so it can only go as high as Mytree.Length + 1.