Search Unity

Int won't update.

Discussion in 'Scripting' started by SomeGuy3, Feb 9, 2016.

  1. SomeGuy3

    SomeGuy3

    Joined:
    Dec 19, 2015
    Posts:
    65
    Hello, so I am trying to make a script that adds and subtracts a certain amount to different resources, this works fine, however I seem to be having an issue with updating the amount that is added/subtracted from each resource. The actually change of the int seems to work fine, but the "public void Converter()" that I use does not change, keeping only the initial int as its int. Is there I way I can make this function update, I need it to be public to access it from a button. Below is my script:
    Code (CSharp):
    1.  using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class Resource_ConverterClick : MonoBehaviour {
    6.  
    7.     private Resource_Inventory myResource_Inventory;
    8.     public Button generate;
    9.     public int AddingResourcesFuel = 5;
    10.     public int AddingResourcesElectric = 20;
    11.  
    12.     void Start()
    13.     {
    14.         myResource_Inventory = GameObject.Find("ResourcesHUD").GetComponent<Resource_Inventory>();
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         if (myResource_Inventory.currentResourceFuel <= 0)
    20.         {
    21.             myResource_Inventory.currentResourceFuel = 0;
    22.             generate.interactable = false;
    23.         }
    24.  
    25.         if (myResource_Inventory.currentResourceFuel > 0)
    26.         {
    27.             generate.interactable = true;
    28.         }
    29.     }
    30.  
    31.     public void ConverterRate5_20()
    32.     {
    33.         AddingResourcesFuel = 5;
    34.         AddingResourcesElectric = 20;
    35.     }
    36.  
    37.     public void ConverterRate15_60()
    38.     {
    39.         AddingResourcesFuel = 15;
    40.         AddingResourcesElectric = 60;
    41.     }
    42.  
    43.     public void Converter()
    44.     {
    45.         myResource_Inventory.AddResourcesFuel(-AddingResourcesFuel);
    46.         myResource_Inventory.AddResourcesElectric(AddingResourcesElectric);
    47.     }
    48. }
    49.  
    For example, my "-AddingResourcesFuel" int keeps with the int 5 even after I make it change to 15, does anyone know how to solve this?
     
  2. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Maybe try...

    Code (CSharp):
    1. public void ConverterRates()
    2. {
    3.     if (AddingResourcesFuel == 15 && AddingResourcesElectric == 60)
    4.     {
    5.         AddingResourcesFuel = 5;
    6.         AddingResourcesElectric = 20;
    7.     }
    8.     else {
    9.         if (AddingResourcesFuel == 5 && AddingResourcesElectric == 20 {
    10.             {
    11.                 AddingResourcesFuel = 15;
    12.                 AddingResourcesElectric = 60;
    13.             }
    14.         }
    15.     }
    16. }
     
  3. SomeGuy3

    SomeGuy3

    Joined:
    Dec 19, 2015
    Posts:
    65
    I've tried what you gave me but it doesn't seem to affect the outcome, the public int during the game still changes but the converter() still keeps the original int, is there maybe someway of updating the int for the resources?
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    @DeanWebb the code you've pasted in the OP works fine here...



    is there anything else updating the values that might be interfering?
     
  5. SomeGuy3

    SomeGuy3

    Joined:
    Dec 19, 2015
    Posts:
    65
    That's odd, there isn't anything else that I can think of that alters it, I'm currently updating unity though, thinking it may be an something wrong with the version I had. I will get back to you when its finished updating.
    Update: Updated unity and now seems to work fine, not sure what the issue was, will keep the script up to help anyone else though.
     
    Last edited: Feb 9, 2016