Search Unity

Int count not registering on greater than it only registeres on less then

Discussion in 'Editor & General Support' started by wazzacoopz, Feb 12, 2020.

  1. wazzacoopz

    wazzacoopz

    Joined:
    Feb 11, 2016
    Posts:
    11
    Hello, I have an issue with greater than or greater equals. I have the count increment when object collides but when calling if greater than a certain number it does not register. It registers only if I am less than a certain number I am starting at 0 so I am always less than. but why does it not register when I am greater than.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Trunk : MonoBehaviour
    6. {
    7.  
    8.     public int trunk;
    9.  
    10.     GameManager camPos1;
    11.  
    12.     bool camCounterR1 = false;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.        
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         camPos1 = GameObject.FindGameObjectWithTag("GameManager").GetComponent<GameManager>();
    24.         trunk = 0;
    25.  
    26.     }
    27.  
    28.     private void OnTriggerEnter(Collider other)
    29.     {
    30.        
    31.         trunk++;
    32.         Destroy(gameObject);
    33.         Debug.Log(trunk++);
    34.  
    35.        
    36.         if (trunk >= 5)
    37.         {
    38.             print("has reached 5");
    39.             camPos1.R2CamPos();
    40.         }
    41.  
    42.  
    43.     }
    44. }
    45.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Linq;
    5.  
    6. public class GameManager : MonoBehaviour
    7. {
    8.     public static List<GameObject> listOfGameObjects = new List<GameObject>();
    9.  
    10.     [SerializeField] public  GameObject[] arrayOfGameObjects = listOfGameObjects.ToArray();
    11.  
    12.     AnimatorController anim;
    13.  
    14.     Transform camTarget;
    15.     Transform camTrans;
    16.     Trunk trunk;
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.        
    22.         camTrans = Camera.main.GetComponent<Transform>();
    23.         Round1();
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     private void Update()
    28.     {
    29.          //GameObject.FindGameObjectWithTag("Trunk").GetComponent<Trunk>();
    30.        
    31.     }
    32.     public void Round1()
    33.     {
    34.         Instantiate(arrayOfGameObjects[0]);
    35.         arrayOfGameObjects[0].transform.position = new Vector3(-5, 3.75f, 0);
    36.         R1CamPos(true);
    37.  
    38.         Instantiate(arrayOfGameObjects[1]);
    39.         arrayOfGameObjects[1].transform.position = new Vector3(-26, 0, 15);
    40.  
    41.         Instantiate(arrayOfGameObjects[2]);
    42.         arrayOfGameObjects[2].transform.position = new Vector3(25, 0, 25);
    43.     }
    44.  
    45.     public void R1CamPos(bool camPos1)
    46.     {
    47.  
    48.        
    49.         camTarget = arrayOfGameObjects[0].GetComponent<Transform>();
    50.         //camTrans.position = camTarget.position = new Vector3(-5, 8, -30);
    51.         camTrans.position = GameObject.FindGameObjectWithTag("CamPos1").transform.position;
    52.     }
    53.  
    54.     public void R2CamPos()
    55.     {
    56.         camTarget = arrayOfGameObjects[1].GetComponent<Transform>();
    57.         //camTrans.position = camTarget.position = new Vector3(-26, 8, -30);
    58.         camTrans.position = GameObject.FindGameObjectWithTag("CamPos2").transform.position;
    59.     }
    60.  
    61.     public void Round2()
    62.     {
    63.         Instantiate(arrayOfGameObjects[3]);
    64.         arrayOfGameObjects[3].transform.position = new Vector3(-63, -7, 0);
    65.         Instantiate(arrayOfGameObjects[4]);
    66.         arrayOfGameObjects[4].transform.position = new Vector3(-26, -7, 15);
    67.         Instantiate(arrayOfGameObjects[5]);
    68.         arrayOfGameObjects[5].transform.position = new Vector3(25, -10, 25);
    69.     }
    70.  
    71.     public void Round3()
    72.     {
    73.         Instantiate(arrayOfGameObjects[6]);
    74.         arrayOfGameObjects[6].transform.position = new Vector3(-63, -7, 0);
    75.         Instantiate(arrayOfGameObjects[7]);
    76.         arrayOfGameObjects[7].transform.position = new Vector3(-26, -7, 15);
    77.         Instantiate(arrayOfGameObjects[8]);
    78.         arrayOfGameObjects[8].transform.position = new Vector3(25, -10, 25);
    79.     }
    80.  
    81.     public void Round4()
    82.     {
    83.         Instantiate(arrayOfGameObjects[9]);
    84.         arrayOfGameObjects[9].transform.position = new Vector3(25, -10, 25);
    85.     }
    86.  
    87.     public void R1()
    88.     {
    89.         anim = FindObjectOfType<AnimatorController>();
    90.         anim.R1();
    91.     }
    92.  
    93.  
    94. }
    95.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If you're talking about the trunk variable, you are resetting it back to 0 every single frame. Not sure how you expect it to get >= to 5, because as soon as Update is called again it is back to 0. Comment out line 24 and see what happens.

    Unrelated, there's a few odd things that confuse me in your code. in Trunk.cs line 33 you're adding to the trunk variable a second time inside a Debug.Log. It is also confusing to use the same name for variables in two different but related scripts but for different types.
     
  3. wazzacoopz

    wazzacoopz

    Joined:
    Feb 11, 2016
    Posts:
    11
    Thanks again Joe, :)