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. Dismiss Notice

Keepings things out of the Update Function

Discussion in 'Scripting' started by dmarku26, Jan 29, 2021.

  1. dmarku26

    dmarku26

    Joined:
    Aug 3, 2020
    Posts:
    23
    Hi, I am building a game and I am trying to keep as much out of the Update function as I can as it overheats the phone. How can I keep the score of an item I am collecting if I don't call it in the update? What is another method? If I remove the line of code from the update and I put it on a specific function, it does not work and the score does not update once the player is in trigger with the object. It works only if I keep track of the score in the update. Any way around this? Thank you!

    Here are the 2 simple scripts:

    **Showing the collected items number:**

    public static int goldCount;
    public Text goldCountText;

    void Start()
    {
    goldCount = PlayerPrefs.GetInt("GoldCount");
    goldCountText.text = goldCount.ToString();
    }

    public void Update()
    {

    goldCountText.text = goldCount.ToString();
    if (goldCount <= 0)
    {
    goldCount = 0;
    }
    }
    }


    **Collecting the item on the trigger:**

    void OnTriggerEnter2D(Collider2D other)
    {
    if (other.CompareTag("Player"))
    {
    GoldCount.goldCount += 1;
    PlayerPrefs.SetInt("GoldCount", GoldCount.goldCount);
    Destroy(gameObject);

    }
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    Stop. That's not a thing. It's just not. If you do X amount of work, it takes X amount of time. Nothing special about Update(). See link at bottom for why.

    To actually get meaningful improvement, don't observe the value and change it every frame.

    Instead, make the value private so nobody else can molest it, then create a function that accepts a number to set (or increase) the value. Through that function should be the ONLY way anyone touches it. That includes Start().

    Inside that function, do two things: update the number, update the text field.

    That way you know none of this code is running until the score changes.

    Here is some timing diagram help:

    https://docs.unity3d.com/Manual/ExecutionOrder.html
     
    Bunny83, Vryken and SparrowGS like this.
  3. dmarku26

    dmarku26

    Joined:
    Aug 3, 2020
    Posts:
    23
    Thank you for the reply but i do not think you understood me correctly. I already know that keeping things like that in the update is not good so that is why i am reaching out for help. I already gave my simple code here in hope someone makes it better with no use of the update so i don't have to observe it like you say, the goldcount every frame. Also i am a beginer so talking about principles is not good for me. I need to see it exactly how it is done to understand it. If not in update then where? How would the code that i gave here look like if we dont use update?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    The key is you "force" everybody to use methods to access the gold count. If it was truly "object oriented" it would be in its own class where you physically could not access it. With the setup above, we can fake it by agreeing with yourself not to ever touch goldCount except via the SetGold() and AddGold() methods below.

    Code (csharp):
    1. private int score; //NOBODY else touches this except these two methods:
    2.  
    3. public void SetGold(int newGold)
    4. {
    5.   goldCount = newGold;
    6.   goldCountText.text = goldCount();
    7. }
    8.  
    9. public void AddGold( int moreGold)
    10. {
    11.   SetGold( goldCount + moreGold);
    12. }
    To be complete you could add a GetGold function too:

    Code (csharp):
    1. public int GetGold()
    2. {
    3.   return goldCount;
    4. }
    Now, in your Start():

    Code (csharp):
    1. int temp = PlayerPrefs.GetInt("GoldCount");
    2. SetGold( temp);
    3.  
     
    Vryken likes this.
  5. dmarku26

    dmarku26

    Joined:
    Aug 3, 2020
    Posts:
    23
    thank you! i will try this method.