Search Unity

My unity update function not updating all if statement conditions with String comparisons

Discussion in 'Documentation' started by wacasce, Apr 30, 2020.

  1. wacasce

    wacasce

    Joined:
    Sep 14, 2017
    Posts:
    4
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class PlayerItems : MonoBehaviour
    7. {
    8.     public static int beacons;
    9.     public static int mines;
    10.     public static int teleport;
    11.     public static int decoys;
    12.     public static int bullets;
    13.     public static string primary;
    14.     public static string secondary;
    15.     public static string ability;
    16.     public static float primaryAmount;
    17.     public static float secondaryAmount;
    18.     public static float abilityAmount;
    19.  
    20.  
    21.    // [SerializeField]
    22.    // Text primaryAmount;
    23.     //[SerializeField]
    24.     //Text secondaryAmount;
    25.    // [SerializeField]
    26.     //Text abilityAmount;
    27.     // Start is called before the first frame update
    28.     void Start()
    29.     {
    30.        //primaryAmount.GetComponent<Text>();
    31.        //secondaryAmount.GetComponent<Text>();
    32.        //abilityAmount.GetComponent<Text>();
    33.         beacons = 0;
    34.         mines = 0;
    35.         teleport = 0;
    36.         decoys = 0;
    37.         bullets = 0;
    38.     }
    39.  
    40.     // Update is called once per frame
    41.     void Update()
    42.     {
    43.  
    44.         if(primary.Equals("mines")){
    45.         //primaryAmount.GetComponent<Text>().text = ""+mines;
    46.         primaryAmount = mines;
    47.         Debug.Log("MinesWorking");
    48.      
    49.         //PrimaryAmountText.amount = mines;
    50.         }
    51.         else{
    52.  
    53.         }
    54.      
    55.             if(secondary.Trim().Equals("beacons")){
    56.             secondaryAmount = beacons;
    57.             Debug.Log("working");
    58.  
    59.         }
    60.         else{
    61.          
    62.         }
    63.         if(ability.Equals("teleport")){
    64.       ///  abilityAmount.text = ""+teleport;
    65.         }
    66.         else{
    67.  
    68.         }
    69.      
    70.     }
    71. }
    ******************************************************************
    Hello guys. The code above is from a game i'm making but i'm having issues with my update function.
    I tried out a lot of solutions but nothing seems to help.
    At first i had public Texts which were updated using the update function of this script, but the second "if statement" sometimes worked and sometimes didnt.
    So my issue is, the first *if(primary.Equals("mines"))* statement works just fine all the time, but the second *if(secondary.Trim().Equals("beacons"))* statement work like for 2/10 times i run the game.
    writing it as *if(secondary.Equals("mines"))* didnt seem to work as well...Any suggestions?