Update() opening scene

I’m making a game that’s about trade, game has 9 different city to trade. I want player to buy a good from a cheaper price and sell it to another city.

using System.Collections;
using System.Collections.Generic;
using System.Threading;
using TMPro;
using UnityEngine;
using UnityEngine.PlayerLoop;
using UnityEngine.UI;

public class WeaponryPrices : MonoBehaviour
{
    public int buyprice0;
    public int sellprice0;
    public float countdown;
    public Text text0;
    public Text selling_text0;

    private void Start()
    {
        UpdateText(text0, buyprice0);
        UpdatesellText(selling_text0, sellprice0);
    }
    void UpdateText(Text tex,int value)
    {
        tex.text = "Buy " + value;
    }
    void UpdatesellText(Text tex, int value)
    {
        tex.text = "Sell " + value;
    }
    private void Update()
    {
        countdown += Time.deltaTime;
        if(countdown > 10f)
        {
            buyprice0 = Random.Range(30,60);
            sellprice0 = buyprice0 - 5;
            UpdateText(text0, buyprice0);
            UpdatesellText(selling_text0, sellprice0);
            countdown -= 10f;
        }
    }


}

When I enter the shop it shows me in the button’s text “button”, after 10 seconds it changes so I noticed that Update() method doesn’t work when player isn’t in the scene.
How can I fix that problem? I want to change prices when the player is in the map.

Two ways I can think of:

  1. Calculate your prizes globally and just display them in the cities.
  2. Store the time when the player leaves a city in the city, then compare it with the current time when the city loads and crunch all the prize development that happened between exiting and entering the city again.
1 Like

So you say I should calculate prices in map and by getcomponent I should take them and display it. Can you write a example script?

It’s less about a single script and more about how you organize things. You can have a Handler Object that’s persistent through all your scenes that handles it. A singleton as a PriceHandler should work well here. You can access it from inside any city and just use the cities as a display.

1 Like

Ok thanks I already had a game controller that attached to every scene I’ll do it with that