Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Variable not being updated

Discussion in 'Scripting' started by SaamBell, Jul 27, 2015.

  1. SaamBell

    SaamBell

    Joined:
    Mar 28, 2014
    Posts:
    128
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class Smelting : MonoBehaviour {
    6.  
    7.     public int bronzeOre;
    8.     public int ironOre;
    9.  
    10.     public int bronzeBar = 0;
    11.  
    12.     List <string> notifications = new List <string>();
    13.     List <float> LifeTime = new List <float>();
    14.  
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.  
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update () {
    23.         //calling ore variables from player script
    24.         bronzeOre = GameObject.Find("Player").GetComponent<Player>().bronzeOre;
    25.         ironOre = GameObject.Find("Player").GetComponent<Player>().ironOre;
    26.  
    27.  
    28.  
    29.  
    30.     }
    31.  
    32.     void OnGUI() {
    33.  
    34.         if(GUI.Button(new Rect(600, 165, 150, 80), "Smelt Bronze Bars"))
    35.         {
    36.             smeltBronze();
    37.  
    38.         }
    39.  
    40.         GUI.Label(new Rect(700, 165, 150, 80), "Bronze Bars" + bronzeBar);
    41.  
    42.  
    43. void smeltBronze()
    44.     {
    45.         if(bronzeOre >=2)
    46.             bronzeBar = bronzeBar + 1;
    47.             bronzeOre = bronzeOre - 2;
    48.      
    49.        
    50.  
    51.  
    52.      
    53.     }
    54.  
    In my player function i have the variable ore that i obtain whenever i successfully mine it (shown by a counter on the left). In my smelting script here i tried to have it so that when i press the button to smelt the ore it would give me +1 bronze bar but -1 bronze ore. However this doesn't seem to be updating in my GUI to the left and keeps allowing me to smelt more and more bars which is not what i want. Any ideas?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    This script will only retrieve the values from the player object and change it locally in this script when the button is pressed.

    The next frame through (in your Update()) you are pulling the original unchanged values into this Smelt script from the Player, so they are reset.

    Generally, you don't want to copy data around like this. You would want to have a single point of update for that data, and if the bronzeBar and bronzeOre "live" on the player (like as his inventory), then either reach in there direclty and change the values, or else make a method in Player that does a "convert ore to bar" and handles the math locally, which would allow you to keep the two variables private, and hence isolate other parts of the code from potential future changes, like if those two variables went off later to live in an Inventory object.
     
  3. SaamBell

    SaamBell

    Joined:
    Mar 28, 2014
    Posts:
    128
    So i should just keep that code in the Player script basically? I was going to originally but i thought i was doing something wrong but having everything in one script as i assumed i should be splitting everything up. Thanks anyways i'll do it the easier way :')
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    He was saying you should avoid duplicating variables in multiple places. You can put the logic (the code) wherever you want it, but when it comes time to access the value of that variable, you should reach into whatever script has it and read it from there, not duplicate it into the current script. For instance, in this case:
    Code (csharp):
    1. private Player player;
    2.  
    3. void Start()
    4. {
    5.     GameObject thePlayer = GameObject.Find("Player");
    6.     if(thePlayer)
    7.         player = thePlayer.GetComponent<Player>();
    8. }
    then when you need the BronzeOre value, just call it like "player.bronzeOre" anywhere in your script.