0

I'm trying to draw some lines in the editor between objects. As I want the system to be dynamic, I'm trying to create it so you can simply insert objects from the editor, and the line will update automatically without editing code.

I've created following code:

public class PathLines : MonoBehaviour {

    public GameObject[] pathPoints;


    // Update is called once per frame
    void Update () {
        for(int i = 0; i < pathPoints.Length; i++)
        {
            if(i != pathPoints.Length)
            {
                Handles.DrawLine(pathPoints[i].transform.position, pathPoints[i + 1].transform.position);
            } else
            {
                Handles.DrawLine(pathPoints[i].transform.position, pathPoints[0].transform.position);
            }

        }
    }
}

And then inserted currently 3 objects to the array in the editor. I do however get the NullException error at the first pathPoints[i], and no matter what I seem to do fixes it. How do I get rid of this error?

(Edit)Flagged as duplicate: My question is concerning getting a NullException error after inserting objects into an array through the editor. Shouldn't this automatically initialize the array, and thereby not cause the Null error?

Jon Dier
  • 127
  • 1
  • 13
  • What is a NullException? Your pathPoints seems to not be initialized when the first update is called. You could initialise it to a empty array and add values when needed. –  Oct 11 '17 at 08:43
  • 1
    identify what is null.... and fix it – BugFinder Oct 11 '17 at 08:43
  • Pu a breakpoint in and check the variables in the quickwatch – Kell Oct 11 '17 at 08:43
  • You're sure it's not on `pathPoints[i + 1]` on the last iteration? – Mathias R. Jessen Oct 11 '17 at 08:44
  • Is pathPoints initialized? By the way, that if-else test is useless, you will never enter the second branch. – Andrea Oct 11 '17 at 08:44
  • @MathiasR.Jessen there is a check for that – Pac0 Oct 11 '17 at 08:44
  • 1
    Aside from the other comments, note that in your loop, `i` will never equal `pathPoints.Length` and thus the body of `if(i != pathPoints.Length)` will never be executed. – Matthew Watson Oct 11 '17 at 08:45
  • 1
    @Pac0 That check is worthless, `i` will never be `pathPoints.Length` given the loop condition. Should be `if(i != pathPoints.Length - 1)` – Mathias R. Jessen Oct 11 '17 at 08:45
  • Also, https://unity3d.com/learn/tutorials/topics/scripting/debugging-unity-games-visual-studio - Run the program under the debugger and see where it's going wrong. – Matthew Watson Oct 11 '17 at 08:46
  • @MathiasR.Jessen right, good catch – Pac0 Oct 11 '17 at 08:46
  • A lot of comments regarding my if/else statement which is not the cause of my question. That part is fixed. I do realise the cause is probably initialization, but wouldn't it automatically initialize with the objects I've inserted into the array in the inspector? – Jon Dier Oct 11 '17 at 08:48
  • 1
    Problem is Handles.DrawLine only for Editors classes, not for MonoBehaviours. If you want to draw lines in editor mode, just replase Handles.DrawLine() to Debug.DrawLine(), with same syntax. If you want to use in-game lines, you need to use GL functiuons. Look there for: https://docs.unity3d.com/ScriptReference/GL.LINES.html – Sergiy Klimkov Oct 11 '17 at 09:03
  • @SergiyKlimkov Ah that worked, thanks =) – Jon Dier Oct 11 '17 at 09:04

1 Answers1

0

The most obvious possible causes are :

  • pathPoints array is not initialized (pathPoints is null)

  • one of the array element is not initialized (one of pathPoints[i] is null]

  • also, your if check should state : i != pathPoints.Length - 1

Pac0
  • 16,761
  • 4
  • 49
  • 67