• Categories
  • Recent
  • Tags
  • Popular
  • Solved
  • Unsolved
  • Users
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (Darkly)
  • No Skin
Collapse
brainCloud Forums

Unity basic setup

Scheduled Pinned Locked Moved Solved APIs
unity
5 Posts 3 Posters 499 Views
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Panagiotis Milios
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
  • P Offline
    P Offline
    Panagiotis Milios
    wrote on last edited by Jonathan
    #3

    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)

    C 1 Reply Last reply
    1
  • C Offline
    C Offline
    Chris Brown
    wrote on last edited by Chris Brown
    #2

    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)

    1 Reply Last reply
    0
  • P Offline
    P Offline
    Panagiotis Milios
    wrote on last edited by Jonathan
    #3

    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)

    C 1 Reply Last reply
    1
  • C Offline
    C Offline
    Chris Brown
    replied to Panagiotis Milios on last edited by
    #4

    @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.

    1 Reply Last reply
    0
  • Paul WinterhalderP Offline
    Paul WinterhalderP Offline
    Paul Winterhalder brainCloudAdmin
    wrote on last edited by
    #5

    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! 🙂

    1 Reply Last reply
    0

  • Login

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • Solved
  • Unsolved
  • Users