Search Unity

Question Gameobject reference becomes none when running unit test or closing/opening the project

Discussion in 'Testing & Automation' started by Erenquin, Feb 12, 2020.

  1. Erenquin

    Erenquin

    Joined:
    Apr 9, 2018
    Posts:
    164
    Hello,
    I have a singleton GameManager with a reference to a VictoryEffect gameobject (which contains a particle effect).

    When I run the unit tests (any unit test), the reference to the VictoryEffect gameobject becomes none.
    upload_2020-2-12_16-51-45.png

    upload_2020-2-12_16-52-10.png -> becomes none.

    The test is like so:
    in Setup
    Code (CSharp):
    1. victoryFX = MonoBehaviour.Instantiate(Resources.Load<GameObject>("_Common/Prefabs/VictoryEffect"));
    2.  
    3. //GameManager
    4. gameManagerGO = new GameObject();
    5. gameManager = gameManagerGO.AddComponent<GameManager>();
    6. scoreManager = gameManagerGO.AddComponent<ScoreManager>();
    7. scoreManager.scoreText = scoreText;
    8. gameManager.victoryFX = victoryFX;
    The unit test:
    Code (CSharp):
    1. public IEnumerator fx_is_played_when_game_is_won()
    2. {
    3.     ExecuteEvents.Execute<IGameOver>(GameManager.Instance.gameObject, null, (x, y) => x.GameWon());
    4.  
    5.     yield return new WaitForSeconds(0.01f);
    6.  
    7.     Assert.IsTrue(victoryFX.activeSelf);
    8. }
     
    antoined73 likes this.
  2. Erenquin

    Erenquin

    Joined:
    Apr 9, 2018
    Posts:
    164
    Last edited: Feb 12, 2020
  3. antoined73

    antoined73

    Joined:
    Feb 23, 2014
    Posts:
    24
    Hi,

    I'm actually having a similar issue when running my unit tests..
    My setup function creates a Gameobject and I can't access its transform from the tests functions themselves..
    And as you, there is actually no valid reason for the instanciated gameobject to be deleted between the setup function and the test that use it.

    I'm under Unity 2019.3.2f1

    EDIT :
    Okay, this is weird but the gameobject I created was referenced as a transform into another script. By giving the gameobject instead of the transform of the object, I have no more this issue.

    In your case, It is like I was giving to the gamemanger a victoryFX reference as a transform. By giving this attribute as a GameObject, it worked for me.
     
    Last edited: Apr 1, 2020