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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

can somebody help me with my errors in my C# script

Discussion in 'Scripting' started by CrimeCrew, Feb 12, 2018.

  1. CrimeCrew

    CrimeCrew

    Joined:
    Feb 9, 2018
    Posts:
    10
    this is my error:

    Assets/Scripts/shops/Shop_Access.cs(71,19): error CS0019: Operator `>=' cannot be applied to operands of type `int' and `string'
    Assets/Scripts/shops/Shop_Access.cs(72,16): error CS0019: Operator `-=' cannot be applied to operands of type `int' and `string'
    Assets/Scripts/shops/Shop_Access.cs(80,19): error CS0019: Operator `>=' cannot be applied to operands of type `int' and `string'
    Assets/Scripts/shops/Shop_Access.cs(81,16): error CS0019: Operator `-=' cannot be applied to operands of type `int' and `string'
    Assets/Scripts/shops/Shop_Access.cs(89,19): error CS0019: Operator `>=' cannot be applied to operands of type `int' and `string'
    Assets/Scripts/shops/Shop_Access.cs(90,16): error CS0019: Operator `-=' cannot be applied to operands of type `int' and `string'

    Assets/Scripts/shops/Shop_Access.cs(98,19): error CS0019: Operator `>=' cannot be applied to operands of type `int' and `string'
    Assets/Scripts/shops/Shop_Access.cs(99,16): error CS0019: Operator `-=' cannot be applied to operands of type `int' and `string'

    this are my scripts:


    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using UnityStandardAssets.Characters.FirstPerson;
    7.  
    8. public class Shop_Access : MonoBehaviour {
    9.    
    10.     public GameObject ShopInventory;
    11.     public GameObject Item01Text;
    12.     public GameObject Item02Text;
    13.     public GameObject Item03Text;
    14.     public GameObject Item04Text;
    15.     public GameObject ItemComplection;
    16.     public GameObject CompleteText;
    17.  
    18.     public GameObject ThePlayer;
    19.  
    20.     public GameObject Item01PriceBox;
    21.     public GameObject Item02PriceBox;
    22.     public GameObject Item03PriceBox;
    23.     public GameObject Item04PriceBox;
    24.  
    25.     public int ItemPurchaseNumber;
    26.     public GameObject NotEnough;
    27.  
    28.     void OnMouseDown () {
    29.         ThePlayer.GetComponent<FirstPersonController> ().enabled = false;
    30.             ShopInventory.SetActive (true);
    31.             Cursor.lockState = CursorLockMode.None;
    32.             Cursor.visible = true;
    33.             GlobalShop.ShopNumber = 1;
    34.         Item01Text.GetComponent<Text> ().text = "" + GlobalShop.Item01;
    35.         Item02Text.GetComponent<Text> ().text = "" + GlobalShop.Item02;
    36.         Item03Text.GetComponent<Text> ().text = "" + GlobalShop.Item03;
    37.         Item04Text.GetComponent<Text> ().text = "" + GlobalShop.Item04;
    38.         Item01PriceBox.GetComponent<Text> ().text = "Kosten: " + GlobalShop.Item01Price;
    39.         Item02PriceBox.GetComponent<Text> ().text = "Kosten: " + GlobalShop.Item02Price;
    40.         Item03PriceBox.GetComponent<Text> ().text = "Kosten: " + GlobalShop.Item03Price;
    41.         Item04PriceBox.GetComponent<Text> ().text = "Kosten: " + GlobalShop.Item04Price;
    42.     }
    43.  
    44.     public void Item01 () {
    45.         ItemComplection.SetActive (true);
    46.         CompleteText.GetComponent<Text> ().text = "Weet je zeker dat je een " + GlobalShop.Item01 + " wilt kopen";
    47.         ItemPurchaseNumber = 1;
    48.     }
    49.     public void Item02 () {
    50.         ItemComplection.SetActive (true);
    51.         CompleteText.GetComponent<Text> ().text = "Weet je zeker dat je een " + GlobalShop.Item02 + " wilt kopen";
    52.         ItemPurchaseNumber = 2;
    53.     }
    54.     public void Item03 () {
    55.         ItemComplection.SetActive (true);
    56.         CompleteText.GetComponent<Text> ().text = "Weet je zeker dat je een " + GlobalShop.Item03 + " wilt kopen";
    57.         ItemPurchaseNumber = 3;
    58.     }
    59.     public void Item04 () {
    60.         ItemComplection.SetActive (true);
    61.         CompleteText.GetComponent<Text> ().text = "Weet je zeker dat je een " + GlobalShop.Item04 + " wilt kopen";
    62.         ItemPurchaseNumber = 4;
    63.     }
    64.     public void CancelTransaction () {
    65.         ItemComplection.SetActive (false);
    66.         ItemPurchaseNumber = 0;
    67.         NotEnough.SetActive (false);
    68.         }
    69.  
    70.     public void CompleteTransaction () {
    71.         if (ItemPurchaseNumber == 1) {
    72.             if (GlobalCash.CurrentCoins >= GlobalShop.Item01Price) {
    73.                 GlobalCash.CurrentCoins -= GlobalShop.Item01Price;
    74.                 ItemComplection.SetActive (false);
    75.             } else {
    76.                 NotEnough.SetActive (true);
    77.             }
    78.         }
    79.  
    80.         if (ItemPurchaseNumber == 2) {
    81.             if (GlobalCash.CurrentCoins >= GlobalShop.Item02Price) {
    82.                 GlobalCash.CurrentCoins -= GlobalShop.Item02Price;
    83.                 ItemComplection.SetActive (false);
    84.             } else {
    85.                 NotEnough.SetActive (true);
    86.             }
    87.         }
    88.  
    89.         if (ItemPurchaseNumber == 3) {
    90.             if (GlobalCash.CurrentCoins >= GlobalShop.Item03Price) {
    91.                 GlobalCash.CurrentCoins -= GlobalShop.Item03Price;
    92.                 ItemComplection.SetActive (false);
    93.             } else {
    94.                 NotEnough.SetActive (true);
    95.             }
    96.         }
    97.  
    98.         if (ItemPurchaseNumber == 4) {
    99.             if (GlobalCash.CurrentCoins >= GlobalShop.Item04Price) {
    100.                 GlobalCash.CurrentCoins -= GlobalShop.Item04Price;
    101.                 ItemComplection.SetActive (false);
    102.             } else {
    103.                 NotEnough.SetActive (true);
    104.             }
    105.         }
    106.     }
    107.     void Update () {
    108.     }
    109. }
    110.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GlobalShop : MonoBehaviour {
    6.  
    7.     public static string Item01;
    8.     public static string Item02;
    9.     public static string Item03;
    10.     public static string Item04;
    11.     public static int ShopNumber;
    12.  
    13.     public static string Item01Price;
    14.     public static string Item02Price;
    15.     public static string Item03Price;
    16.     public static string Item04Price;
    17.  
    18.     void Update () {
    19.         if (ShopNumber == 1)
    20.         {
    21.             Item01 = "Wood Block";
    22.             Item01Price = "10";
    23.             Item02 = "Black Feather";
    24.             Item02Price = "10";
    25.             Item03 = "Red Postion";
    26.             Item03Price = "20";
    27.             Item04 = "Blue Postion";
    28.             Item04Price = "15";
    29.         }
    30.         if (ShopNumber == 2)
    31.         {
    32.             Item01 = "Iron Block";
    33.             Item02 = "Black Feather";
    34.             Item03 = "Red Postion";
    35.             Item04 = "";
    36.         }
    37.     }
    38. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class GlobalCash : MonoBehaviour {
    7.  
    8.     public GameObject InventoryDisplay;
    9.     public GameObject ShopDisplay;
    10.  
    11.     public static int CurrentCoins = 100;
    12.     public int LocalCoins;
    13.  
    14.     void Update () {
    15.         LocalCoins = CurrentCoins;
    16.         InventoryDisplay.GetComponent<Text> ().text = "Coins: " + LocalCoins;
    17.         ShopDisplay.GetComponent<Text> ().text = "Coins: " + LocalCoins;
    18.     }
    19. }

     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    It's pretty clear. You can't compare an int and a string. If your string prices are int values, you need to convert them.

    Example, if the value is 100, the string version is "100"

    so, do int.parse(stringVariable) and compare that. Note that normally I suggest using tryParse, but if you know the values are ints, you should be fine. However, it has to be a value that can convert to an int. You can't have "$100" for example as that wouldn't convert.
     
    UnrealByte likes this.
  3. Fido789

    Fido789

    Joined:
    Feb 26, 2013
    Posts:
    343
    You should probably make your price variables numeric, use integers or decimals or floats, whatever will be best for your game.

    Code (CSharp):
    1. public static int Item01Price;
    2. public static int Item02Price;
    3. public static int Item03Price;
    4. public static int Item04Price;
    You can then easily convert them to strings, but the other way is a bit tricky.
     
  4. CrimeCrew

    CrimeCrew

    Joined:
    Feb 9, 2018
    Posts:
    10
    Thank you Fido789 you are great. The errors are gone.
     
  5. Fido789

    Fido789

    Joined:
    Feb 26, 2013
    Posts:
    343
    You are welcome!
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Also for your own sanity, learn about collections. Specifically arrays and lists.