googleanalytics / google-analytics-plugin-for-unity

Google Analytics plugin for the Unity game creation system

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Returning to the initial scene with the root GAv4 object creates duplicate instances

jamesvecore opened this issue · comments

The usage of DontDestroyOnLoad() in the script GoogleAnalyticsV4.cs seems incorrect and leads to duplicate GAv4 objects when the scene is loaded again.

To Reproduce:

  • Add the GAv4 object to the root of hierarchy of the initial scene.
  • Run the initial scene and then request the same scene to be loaded again.
  • Notice there are now two instances of GAv4 in the root of the hierarchy.

Each time you reload the scene that contained the GAv4 object, a duplicate copy will be created. To fix this, the awake method needs to deal with the call to DontDestoryOnLoad():

void Awake() 
{
    if (instance == null)
    {
        instance = this;
        DontDestroyOnLoad(gameObject);
    }
    else if (instance != this)
    {
        // someone else is the master instance, so kill itself
        Destroy(gameObject);
        return;
    }

    InitializeTracker ();
...

In the code above the duplicate objects are automatically deleted before they call InitializeTracker ().

I don't see this v4 source code in the repo otherwise I would do a pull request. I used the V4 unity package to get the source code into Unity.

I'm having exactly the same issue. My game consists of only one scene. The scene is reloaded after each "level" completion. After each level I'm getting another instance of GAv4 in the hierarchy.

Me too (Registered to follow issue...)

Yepp, having the same issue.
@jamesvecore: thank you for providing a fix!