0
\$\begingroup\$

I just installed Android logcat to debug my game from Unity's package manager. I don't fully understand how it works or in what order the logs are shown relative to my code.

The problem I have is that my game works perfectly fine without any errors on the Unity editor, but when testing it on my build, it doesn't work as expected.

When I used the Android Logcat to see what's wrong, it turns out that one of my code's functions does not fully execute (it stops halfway through), and at that point there is a NullReferenceException error. But how do I identify the exact line of code the error comes from?

Here's a screenshot of my logcat:

Unity Android Logcat

EDIT: Here is the method that calls GameEnvironment.ObstacleSpawnPoints()

if (CurrentLevel().Equals(1) || levelData.Environment.ObstacleSpawnPoints().IsNullOrEmpty()) return;
    
    int numberOfObstaclesToSpawn = levelData.Environment.ObstacleSpawnPoints().Length;

    for (int i = 0; i < numberOfObstaclesToSpawn; i++)
    {
        int tagIndex = UnityEngine.Random.Range(0, levelData.Environment.ObstaclePoolTags().Length);
        levelData.m_ObjectPooler.SpawnFromPool(levelData.Environment.ObstaclePoolTags()[tagIndex], levelData.Environment.ObstacleSpawnPoints()[i].position, Quaternion.identity);
    }

This is GameEnvironment.ObstacleSpawnPoints()

public Transform[] ObstacleSpawnPoints()
{
    if (properties.ObstacleSpawnPoints.Equals(null)) return null;
    Transform[] spawnPoints = new Transform[properties.ObstacleSpawnPoints.childCount];
    for (int i = 0; i < spawnPoints.Length; i++)
        spawnPoints[i] = properties.ObstacleSpawnPoints.GetChild(i);
    return spawnPoints;
}
\$\endgroup\$
2
  • \$\begingroup\$ Error messages should be shared as text, not as images. It may also help to show the code of the GameEnvironment.ObstacleSpawnPoints() method and where it's called, as we can usually diagnose NullReferenceExceptions based on that. \$\endgroup\$
    – DMGregory
    Commented Feb 18, 2022 at 16:49
  • \$\begingroup\$ @DMGregory I've edited them in. \$\endgroup\$ Commented Feb 18, 2022 at 17:25

2 Answers 2

0
\$\begingroup\$

There's only one thing that could possibly be null in this code, so you don't need to get the line number from Logcat.

spawnPoints cannot be null because we define and assign it inside this method itself. So the only thing we access that could possibly throw a NullReferenceException is properties.ObstacleSpawnPoints.

"But I check whether that's null before using it!"

No, you don't. You try to use it to check whether it itself is null.

.Equals() is a member function. To invoke a member function, you need a valid instance to call it on. If you have a null, this is not a valid instance and will fail.

Consider making your null check if (properties.ObstacleSpawnPoints == null) instead.

Or, consider structuring your code so that properties.ObstacleSpawnPoints cannot be null when this code is called. We'd need to see more of your code to advise on how it's ending up null in the first place though.

\$\endgroup\$
2
  • \$\begingroup\$ Thank you for this answer, it inspired me to return a method list instead of array on properties.ObstacleSpawnPoints, and it worked fine. I still don't understand why the initial problem ran fine on the editor without any errors but didn't on my phone. \$\endgroup\$ Commented Feb 19, 2022 at 8:52
  • \$\begingroup\$ You have not given us a Minimal Complete Verifiable Example to help you diagnose that. Typical causes are order of execution differences for methods like Start - this code could be running before the code that initialized the spawn points. \$\endgroup\$
    – DMGregory
    Commented Feb 19, 2022 at 9:37
0
\$\begingroup\$

You can get the line number of a C# Exception when you build with IL2CPP when using Sentry's SDK: https://docs.sentry.io/platforms/unity/configuration/il2cpp/

The line number will show up on Sentry's dashboard (not on logcat) because it requires the server to process the stack trace with the debug symbols that it collects during build time.

But that means you'll get legible stack traces and line numbers on your production game. From your real users.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .