How to take over a variable when you move through the unity scene

Asked 1 years ago, Updated 1 years ago, 41 views

Is there a way to take over the variable even if I move the scene?

Let's say there are two scenes (shop and main)
Suppose you spend money at a shop and buy items.
Take over the effects mainly. Is there a way?

c# unity3d

2022-09-30 15:32

1 Answers

Creating static objects/classes and managing currencies and items there is an easy way to do this.

For example, create a class like this

class GameData{
    static int coins;
    static readonly list<int>items=new list<int>();

    public static bool BuyItem(intemId){
        // purchasing process
        if(coins>=10){
            // I bought an item.
            items.Add(itemId);
            coins-=10;
            return true;
        } else{
            // be short of money
            return false;
        }
    }
}

If you add this script to the main scene, other scripts will not be able to

GameData.BuyItem(ItemId.hoge);

and so on, regardless of the scene.


2022-09-30 15:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.