Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error when switching splines
#1
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:

Code:
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

Code:
    [SerializeField]
    private List<CurvySpline> alternateRoutes;

....

    private void ControlPointReached(CurvySplineMoveEventArgs arg0)
    {
        Debug.Log($"Control Point {arg0.ControlPoint.name} 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);
            }
        }
    }
Reply
#2
Hi
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.
Reply
#3
Yes, that worked great... no more errors.  Thanks very much for your help, I appreciate it! Big Grin
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Video How do I merge splines only if some control points are at the sam position? arcade.autumn 2 3 8 hours ago
Last Post: arcade.autumn
  Way to make gameobject follow splines 'human-ly'? yun844 3 5 01-17-2023, 10:18 AM
Last Post: _Aka_
Rainbow Parallel Splines etoreo 1 5 01-05-2023, 09:13 AM
Last Post: _Aka_
Exclamation Stack overflow when editing prefab containing splines rhys_vdw 4 11 11-30-2022, 11:38 AM
Last Post: _Aka_

Forum Jump: