Access and pass variables between multiple scripts

Hi,

this may be a noobish question, but I have tried searching for this problem for 2 days now and Google isn’t my friend. Here’s my setup:

I’m trying to make a game, where I use a gameobject that go across multiple scenes, that contains variables (both float, bool’s, ints etc.)such as lives, coins and so on. I also have gameobject for each scene that holds variables for specific for only the current scene. All set to be public/static.

My problem is, I can’t send or receive data from these scripts and use in scripts in my scenes. Etc. I have a script for money, and when the coin collides with player, it won’t send either coins +=1; or coins ++; to the main script with coins;

Also I can’t fetch bools or floats from the main script either to check and use to alter gameplay.

Please help me. I’m not a programmer by trade, but I have some insight of coding afterall. I use c++ in Unity 4.6

First, you need to make your GameObject that needs to stay alive across scenes persistent. Check this out:
Object.DontDestroyOnLoad

Then, you’ll need to grab this from the scripts you have in each scene. Since it is a GameObject not known by them since it is coming from another scene, you should search for it:

    YourScript reference;
    
    void Start(){
    reference = GameObject.Find("TheLivingObject").GetComponent<YourScript>();
    }

From this point on you can call methods as you know it on reference.

@hexagonius;

I’d swear I’ve tested this out before, perhaps some similar lines. But 2 days of struggle rectified in 10 minutes of testing. Received and sended an int variable (coins) with succes. Thanks a lot. :slight_smile:

I’m tired so I’m going to ask anyway - this goes for all variables right? E.g. setting bools to true/false etc. ?