Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Baffling MonoBehaviour behavior

Discussion in 'Scripting' started by juliang, Feb 6, 2023.

  1. juliang

    juliang

    Joined:
    Nov 25, 2008
    Posts:
    98
    In my GO I have an array:

    private GameObject[] joints;


    And in Start() there’s:

    joints = new GameObject[12];
    for (int i = 0; i < joints.Length; i++)
    joints[i] = GameObject.Instantiate(jointPrefab, Vector3.zero, quaternion.identity);

    // some stuff, then at the end of the method:
    for (int i=0; i<12; i++)
    if (joints[i] == null)
    Debug.LogException(new UnityException($"{nameof(Start)}() {i} IS NULL"));


    Then, in Update(), right at the beginning, there’s

    for (int i=0; i<joints.Length; i++)
    if (joints[i] == null)
    Debug.LogException(new UnityException($"{nameof(Update)}() {i} IS NULL"));


    There is nothing logged from Start(), which makes me think the GO creation went with no problem. But as soon as Update() gets going every entry in the array is null. So in between Start() and Update() the array has gotten wiped out. I’m not even sure where to set a breakpoint. What is the problem?

    This is with Unity Pro 2021.3.18f1 on a MacBook Pro.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Most likely you simply have more than one copy of this script in the scene. On one of the copies of the script,
    jointPrefab
    is null.

    Scroll all the way to the top of your console and check the first couple of exceptions.
     
  3. juliang

    juliang

    Joined:
    Nov 25, 2008
    Posts:
    98
    Ooh, interesting! Can't find any duplications, though. Also there's only one stream of log messages. A duplicate copy of the script would be duplicating the messages.
    In scrutinizing the console I did find one weird thing - entry 0 in the array is still good. It's all the others that are getting messed with.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    You'd have to show more code and show what debugging steps you took, as well as the console output you are seeing.

    I notice you are in general not using brackets with your for loops Perhaps an error there?
     
  5. juliang

    juliang

    Joined:
    Nov 25, 2008
    Posts:
    98
    Tried it and no change, but that's good. One liners have always been legal. If it did change behavior that would be a bug in the C# compiler.
     
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,525
    Are you sure you don't have anything in your current scene or on the prefabs themselfs that could destroy them? Maybe they are destroyed right away after the first frame. If the prefab has a script attached, try adding a Debug.Log call to the OnDestroy callback. That way you know when a prefab instance is destroyed. It also gives you the stacktrace, so you can follow it backwards to see what may have destroyed it.
     
  7. juliang

    juliang

    Joined:
    Nov 25, 2008
    Posts:
    98
    No scripts on the prefabs, they're just meshes and renderers and colliders.

    More info: I made a couple more GO arrays to mirror the original one. They likewise come out of Start() properly filled in, and likewise are nulled out by the first Update(). And again with element 0 being the only OK one.
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    You're going to have to share more details here, otherwise forum-goers will be of limited help since we're largely in the dark. Sharing the full script would be a good start.
     
  9. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    326
    Debug.Log
    is not a serious way to fix bugs. It's good for quick checks of values or triggers, but for real bugs, attach Unity to your IDE, put breakpoints in each method (Start, Update...), debug it (F5 in Visual Studio) then step into your code line by line (F10 in Visual Studio) and check the values of your array in each step. You'll find your bug in no time.
     
  10. juliang

    juliang

    Joined:
    Nov 25, 2008
    Posts:
    98
    I would like to, but this app HAS to run on the phone because of the tech it uses, which means doing a build and installing it. I actually use Rider, and I haven't seen a way to connect their debugger to the executing app on the phone. Seems like the only way to get info is to put messages in the Xcode log.
     
  11. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
  12. juliang

    juliang

    Joined:
    Nov 25, 2008
    Posts:
    98
  13. juliang

    juliang

    Joined:
    Nov 25, 2008
    Posts:
    98
    Found it!
    The key factor is that an exception stops execution of Start(). One of the packages I was using was throwing an exception. It wasn't really an issue, except that it happened before my code initialized everything, so the array of GO's got its first entry but then was interrupted, and the verification code never even got executed. Keeping that package happy let Start() do its job.
    Thank you everyone for your help!
     
    Nad_B likes this.