SebLague / Path-Creator

Path creation asset for Unity game development
https://assetstore.unity.com/packages/tools/utilities/b-zier-path-creator-136082
MIT License
1.8k stars 314 forks source link

NullReferenceException #123

Open Fire-Cube opened 2 years ago

Fire-Cube commented 2 years ago

When I create an empty object and add the path creator script, I get the following error messages.

NullReferenceException: Object reference not set to an instance of an object
PathCreation.BezierPath.CalculateBoundsWithTransform (UnityEngine.Transform transform) (at Assets/PathCreator/Core/Runtime/Objects/BezierPath.cs:409)
PathCreationEditor.PathEditor.DrawBezierPathSceneEditor () (at Assets/PathCreator/Core/Editor/PathEditor.cs:433)
PathCreationEditor.PathEditor.OnSceneGUI () (at Assets/PathCreator/Core/Editor/PathEditor.cs:289)
(wrapper dynamic-method) System.Object.lambda_method(System.Runtime.CompilerServices.Closure,UnityEditor.Editor)
UnityEditor.SceneView.CallOnSceneGUI () (at <7d481861b3d34328a9633ade22dcedd6>:0)
UnityEditor.SceneView.HandleSelectionAndOnSceneGUI () (at <7d481861b3d34328a9633ade22dcedd6>:0)
UnityEditor.SceneView.DoOnGUI () (at <7d481861b3d34328a9633ade22dcedd6>:0)
UnityEditor.SceneView.OnSceneGUI () (at <7d481861b3d34328a9633ade22dcedd6>:0)
UnityEngine.UIElements.IMGUIContainer.DoOnGUI (UnityEngine.Event evt, UnityEngine.Matrix4x4 parentTransform, UnityEngine.Rect clippingRect, System.Boolean isComputingLayout, UnityEngine.Rect layoutSize, System.Action onGUIHandler, System.Boolean canAffectFocus) (at <107446276b724792a53cd389e03a278b>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)
NullReferenceException: Object reference not set to an instance of an object
PathCreationEditor.PathEditor.DrawBezierPathInspector () (at Assets/PathCreator/Core/Editor/PathEditor.cs:119)
PathCreationEditor.PathEditor.OnInspectorGUI () (at Assets/PathCreator/Core/Editor/PathEditor.cs:89)
UnityEditor.UIElements.InspectorElement+<>c__DisplayClass62_0.<CreateIMGUIInspectorFromEditor>b__0 () (at <795c477f4b8b45c2b2848ba27f9a227b>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)
hendryluk commented 1 year ago

Having the same error on Unity 2022.1.21f1 Mac Silicon.

thangld322 commented 1 year ago

Having the same error on Unity 2022.2.1f1 Ubuntu 22.04

pvharmo commented 1 year ago

I had the same issue with unity 2022.2.7f1 on Windows. The issue seems to be at line 47 of PathCreatorData.cs it checks if the bezier path is null. For some reason it seems to already be initialized but the points array in BezierPath was not initialized. To fix it I added the following lines in BezierPath.cs:

/// Is the points array null?
public bool HasPoints
{
    get
    {
        return points != null;
    }
}

and then i added the check at line 47 of PathCreatorData.cs:

- if (_bezierPath == null) {
+ if (_bezierPath == null || !_bezierPath.HasPoints) {
    CreateBezier (Vector3.zero, defaultIs2D);
}

I'm not very familiar with the codebase so i'm not sure if it will have impacts somewhere else in the code.

jpedretti commented 1 year ago

If BezierPath having zero points is a valid scenario then I would change the method name from HasPoints to IsValid or IsPointsNull. If it is not a valid scenario then I think it is worth adding an empty check after the null check. It is an old bug. I think I'll create a pull request. @SebLague can you confirm if it makes sense for the BezierPath to have an empty points list?

edit: it looks like a problem with newer unity versions. when the editor is instantiating a new PathCreatorData it is probably setting the invalid BelzierPath instance through the [SerializeField], I removed it to test and the _bezierPath is null during PathCreatorData initialization. image