Unity basic setup



  • Hello,

    Unity noob here, trying to understand how to properly use Braincloud in Unity for different scenes. So far i've attached the following script in an empty GameObject and it succesfully passes through scenes.

    public class BCConfig : MonoBehaviour
    {
        private BrainCloudWrapper _bc;
    
        public BrainCloudWrapper GetBrainCloud()
        {
            return _bc;
        }
    
        void Awake()
        {
            DontDestroyOnLoad(this.gameObject);
            _bc = this.gameObject.AddComponent<BrainCloudWrapper>();
    
            _bc.WrapperName = gameObject.name;    // Optional: Set a wrapper name
            _bc.Init();      // Init data is taken from the brainCloud Unity Plugin   
        }
    
    }
    

    Now in a different scene, in the start() function i use this: _bc = BCConfig.GetBrainCloud(); but i get an error (Object reference not set to an instance of an object).

    What am i missing here? Would using a static BraincloudWrapper work aswell?



  • GetBrainCloud() isn't static, which means in your other script what is BCConfig? Looks like you're missing a reference to it from what I can see. Even though your GameObject may carry over, you'll still need a reference to it.

    As you mentioned, making the variable static would work. I use this in our apps, and just assign the value in awake.

    public static BrainCloudWrapper brainCloudWrapper;

    Then in your case, you'd just reference it with BCConfig._bc; (using your variable name)



  • Yes, that solved the problem. Thanks for your input Chris. Although, i'm always "scared" of using statics so i ended up doing it in a kindly different way.

    Since the forum is new, i'll post the solution here in case any future newbies like myself get stuck in the same problem. It's more of a Unity problem and less of a BrainCloud problem but thanks to BC support, i understood what i was doing wrong.

    In order to use BrainCloud in multiple scenes you can either use a static variable like Chris said (it's easier doing it like that) or you need to create a GameObject and attach to it the script below.

    using UnityEngine;
    
    public class BCConfig : MonoBehaviour
    {
        private BrainCloudWrapper _bc;
    
        public BrainCloudWrapper GetBrainCloud()
        {
            return _bc;
        }
    
        void Awake()
        {
            
            gameObject.name = "BrainCloud"; // I assign the name Braincloud to my game object to use it in the Find() function
            _bc = gameObject.AddComponent<BrainCloudWrapper>();
            _bc.WrapperName = "MyWrapper"; // optionally set a wrapper-name
            _bc.Init(); // extra data, such as: _appId, _secret and _appVersion, is taken from the brainCloud Unity Plugin. See Installation Guide above
            DontDestroyOnLoad(gameObject);
        }
    
    }
    
    

    In some other scene where you need access to the _bc variable you just simply do the following on the start method:

     _bc = GameObject.Find("BrainCloud").GetComponent<BCConfig>().GetBrainCloud();
    

    edit: small fix made to the example code, by a brainCloud mod (@Jonathan)



  • @Panagiotis-Milios

    Nothing wrong with using Static, just have to know when to use it and when not to.
    I avoid GameObject.Find, but some are ok with it at the start of a scene.

    Glad you have a solution that works for you.


  • brainCloud

    Cool to see the forums working! 🙂

    I've marked @Panagiotis-Milios more detailed explanation as the accepted solution, but kudos to you @Chris-Brown for pointing it out, and @Panagiotis-Milios for providing the detailed example. Thanks a bunch! 🙂


Log in to reply