Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error when switching splines
Hi there.  I'm currently using Curvy 8.2.2 in Unity 2021.3.10f1 for moving my ships in my game.  I have a few prefabs of splines, starting with one created in the scene and the others contained in a list of alternate paths on a script.  In the script, I check in the ControlPointReached event the control points along the way to determine if I want to switch to a different spline from that location using the SwitchTo method on the spline controller.  It works as expected, but I do receive the following error shortly after calling the SwitchTo method:

NullReferenceException: Object reference not set to an instance of an object
FluffyUnderware.Curvy.CurvySplineSegment.Interpolate (System.Single localF, UnityEngine.Space space) (at Assets/Standard Assets/ToolBuddy/Assets/Curvy/Scripts/Splines/CurvySplineSegment.cs:977)
FluffyUnderware.Curvy.CurvySplineSegment.InterpolateAndGetTangent (System.Single localF, UnityEngine.Vector3& position, UnityEngine.Vector3& tangent, UnityEngine.Space space) (at Assets/Standard Assets/ToolBuddy/Assets/Curvy/Scripts/Splines/CurvySplineSegment.cs:1109)
FluffyUnderware.Curvy.Controllers.SplineController.GetInterpolatedSourcePosition (System.Single tf, UnityEngine.Vector3& interpolatedPosition, UnityEngine.Vector3& tangent, UnityEngine.Vector3& up) (at Assets/Standard Assets/ToolBuddy/Assets/Curvy/Scripts/Controllers/SplineController.cs:488)
FluffyUnderware.Curvy.Controllers.CurvyController.ComputeTargetPositionAndRotation (UnityEngine.Vector3& targetPosition, UnityEngine.Vector3& targetUp, UnityEngine.Vector3& targetForward) (at Assets/Standard Assets/ToolBuddy/Assets/Curvy/Scripts/Controllers/CurvyController.cs:730)
FluffyUnderware.Curvy.Controllers.SplineController.ComputeTargetPositionAndRotation (UnityEngine.Vector3& targetPosition, UnityEngine.Vector3& targetUp, UnityEngine.Vector3& targetForward) (at Assets/Standard Assets/ToolBuddy/Assets/Curvy/Scripts/Controllers/SplineController.cs:593)
FluffyUnderware.Curvy.Controllers.CurvyController.InitializedApplyDeltaTime (System.Single deltaTime) (at Assets/Standard Assets/ToolBuddy/Assets/Curvy/Scripts/Controllers/CurvyController.cs:689)
FluffyUnderware.Curvy.Controllers.SplineController.InitializedApplyDeltaTime (System.Single deltaTime) (at Assets/Standard Assets/ToolBuddy/Assets/Curvy/Scripts/Controllers/SplineController.cs:568)
FluffyUnderware.Curvy.Controllers.CurvyController.ApplyDeltaTime (System.Single deltaTime) (at Assets/Standard Assets/ToolBuddy/Assets/Curvy/Scripts/Controllers/CurvyController.cs:1017)
FluffyUnderware.Curvy.Controllers.CurvyController.Update () (at Assets/Standard Assets/ToolBuddy/Assets/Curvy/Scripts/Controllers/CurvyController.cs:608)

Here's a snippet of my code that I'm using.  From the docs and having a look at the code, SwitchTo looks like it requires the spline controller to be running when doing the switch, which I do have it running.  Wondering if I'm missing something on doing the switch correctly?  Any thoughts?  Thanks for your help.  Smile

    private List<CurvySpline> alternateRoutes;


    private void ControlPointReached(CurvySplineMoveEventArgs arg0)
        Debug.Log($"Control Point {} reached.");

        if (Random.Range(0, 100) >= 75)
            if (alternateRoutes.Count > 0)
                var newPath = alternateRoutes[0];
                if (alternateRoutes.Count > 1) newPath = alternateRoutes[Random.Range(0, alternateRoutes.Count)];

                var pathInstance = Instantiate<CurvySpline>(newPath);
                pathInstance.transform.position = arg0.ControlPoint.transform.position;

                _controller.SwitchTo(pathInstance, 0, 1f);
From looking at the code, I think the issue comes Unity not calling the Start method on objects right after the Instantiate call, but at the frame's end or next frame's start. With Start not being called, the spline's internal data is not setup yet, leading to the exception being thrown. Try the following:
Just after the Instantiate call, please call pathInstance.Start(). Did this fix the issue?
Have a nice day
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Yes, that worked great... no more errors.  Thanks very much for your help, I appreciate it! Big Grin
You are welcome
If and when you feel like it, please leave a review for the asset, that helps a lot.
Have a nice day
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.

Possibly Related Threads…
Thread Author Replies Views Last Post
  snap to the curve created by the curvy splines segment points ShiroeYamamoto 3 11 04-02-2024, 02:24 PM
Last Post: _Aka_
Bug Changing spline connection in inspector causes splines to revert to defaults lacota 3 6 03-18-2024, 07:55 PM
Last Post: _Aka_
  Using Unity's SplineContainer in Curvy Splines dlees9191 3 15 02-26-2024, 09:49 AM
Last Post: _Aka_
  Distance travelled across multiple splines jh092 1 7 02-23-2024, 09:44 AM
Last Post: _Aka_

Forum Jump: