Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creting SplineControllers thru code generating errors
#1
Hey,

I'm creating lots of vehicle objects with SplineControllers assigned to each. Below code is how I'm creating them. Objects are being created and scene is running fine but there are lots of errors in the console. I wonder if this is how they should be created. I'm precreating them inside Awake then activating on SpawnStart which is where exceptions are triggering (block.SetActive(true)).

I updated the version to 7 but still getting those errors. 

Code:
       
void SpawnInit()
       {
           offsets = new Offsets[] {
               new Offsets() { angle = 84.0f, radius = 5.0f },
               new Offsets() { angle = 0.0f, radius = 0.0f },
               new Offsets() { angle = -84.0f, radius = 5.0f }
           };

           allBlocks = new List<GameObject>();

           //spline.Refresh();
           float splineLength = 1f;

           newDistance = initSpacing;
           while (newDistance < splineLength)
           {
               GameObject block = new GameObject();
               block.SetActive(false);
               blockSplineController = block.AddComponent<SplineController>();
               blockSplineController.Spline = spline;
               blockSplineController.UseCache = true;
               blockSplineController.Speed = carSpeed;
               blockSplineController.PlayAutomatically = false;
               blockSplineController.RelativePosition = newDistance;

               offsetData = offsets[Random.Range(0, 3)];
               blockSplineController.OffsetAngle = offsetData.angle;
               blockSplineController.OffsetRadius = offsetData.radius;
               blockSplineController.OffsetCompensation = true;

               GameObject prefab = Instantiate(ObstacleBlocks[Random.Range(0, ObstacleBlocks.Length)]);
               prefab.transform.parent = block.transform;

               allBlocks.Add(block);
               
               newDistance += spacing;
           }
       }

void SpawnStart()
{
foreach (GameObject block in allBlocks)
{
               block.SetActive(true);
               blockSplineController = block.MMGetComponentNoAlloc<SplineController>();
               blockSplineController.Play();
}
}

NullReferenceException: Object reference not set to an instance of an object
FluffyUnderware.Curvy.Controllers.CurvyController.Initialize () (at Assets/Plugins/Curvy/Controllers/CurvyController.cs:826)
FluffyUnderware.Curvy.Controllers.CurvyController.OnEnable () (at Assets/Plugins/Curvy/Controllers/CurvyController.cs:572)
UnityEngine.GameObject:SetActive(Boolean)
FluffyUnderware.Curvy.Examples.SpawnObstacles:SpawnStart() (at Assets/Scripts/SpawnObstacles.cs:92)
FluffyUnderware.Curvy.Examples.SpawnObstacles:OnMMEvent(TopDownEngineEvent) (at Assets/Scripts/SpawnObstacles.cs:118)
MoreMountains.Tools.MMEventManager:TriggerEvent(TopDownEngineEvent) (at Assets/TopDownEngine/ThirdParty/MoreMountains/MMTools/Events/MMEventManager.cs:162)
MoreMountains.TopDownEngine.TopDownEngineEvent:Trigger(TopDownEngineEventTypes, Character) (at Assets/TopDownEngine/Common/Scripts/Managers/GameManager.cs:59)
MoreMountains.TopDownEngine.ActionButton:StartAction() (at Assets/Scripts/ActionButton.cs:23)
UnityEngine.EventSystems.EventSystem:Update() (at /Applications/Unity/Hub/Editor/2019.3.14f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:377)


NullReferenceException: Object reference not set to an instance of an object
FluffyUnderware.Curvy.Controllers.SplineController.InvokeEventHandler (FluffyUnderware.Curvy.Controllers.CurvySplineMoveEvent event, FluffyUnderware.Curvy.Controllers.CurvySplineMoveEventArgs eventArgument, FluffyUnderware.Curvy.CurvyPositionMode positionMode, FluffyUnderware.Curvy.CurvySplineSegment& postEventsControlPoint, System.Boolean& postEventsIsControllerOnControlPoint, System.Single& postEventsControlPointPosition) (at Assets/Plugins/Curvy/Controllers/SplineController.cs:927)
FluffyUnderware.Curvy.Controllers.SplineController.HandleReachingNewControlPoint (FluffyUnderware.Curvy.CurvySplineSegment controlPoint, System.Single controlPointPosition, FluffyUnderware.Curvy.CurvyPositionMode positionMode, System.Single currentDelta, System.Boolean& cancelMovement, FluffyUnderware.Curvy.CurvySplineSegment& postEventsControlPoint, System.Boolean& postEventsIsControllerOnControlPoint, System.Single& postEventsControlPointPosition) (at Assets/Plugins/Curvy/Controllers/SplineController.cs:909)
FluffyUnderware.Curvy.Controllers.SplineController.EventAwareMove (System.Single distance) (at Assets/Plugins/Curvy/Controllers/SplineController.cs:779)
FluffyUnderware.Curvy.Controllers.SplineController.Advance (System.Single speed, System.Single deltaTime) (at Assets/Plugins/Curvy/Controllers/SplineController.cs:487)
FluffyUnderware.Curvy.Controllers.CurvyController.InitializedApplyDeltaTime (System.Single deltaTime) (at Assets/Plugins/Curvy/Controllers/CurvyController.cs:675)
FluffyUnderware.Curvy.Controllers.SplineController.InitializedApplyDeltaTime (System.Single deltaTime) (at Assets/Plugins/Curvy/Controllers/SplineController.cs:531)
FluffyUnderware.Curvy.Controllers.CurvyController.ApplyDeltaTime (System.Single deltaTime) (at Assets/Plugins/Curvy/Controllers/CurvyController.cs:1024)
FluffyUnderware.Curvy.Controllers.CurvyController.Update () (at Assets/Plugins/Curvy/Controllers/CurvyController.cs:605)
Reply
#2
Hi,
Indeed, I could reproduce the issue. It seems that when you instantiate CurvyController from code, its members that are events are not set to a non null value. This is not the case for controllers instantiated through the Unity editor. I will take a look at this very soon, and will keep you updated. Until then, if events are not something you use, you can either comment the lines that throw the error, or add a ? before the Invoke call, like this:
someEvent?.Invoke(someEventArguments);
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
After further investigation, I can say for sure this is a Unity bug. I send them a bug report. Waiting for their answer
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#4
(05-30-2020, 07:33 PM)_Aka_ Wrote: After further investigation, I can say for sure this is a Unity bug. I send them a bug report. Waiting for their answer

Ok. After adding ? to those lines the errors arent showing up but I'm getting unexpected results when game is running. 

Yea please keep me updated on the bug. My unity version is 2019.3.14f and there is a new one recently came out 2019.3.15f. But I don't know if it has a fix.
Reply
#5
What do you mean by unexpected results?
To clarify:
Code:
someEvent?.Invoke(someEventArguments)
is a shorter way to write

Code:
if(someEvent!= null)

someEvent.Invoke(someEventArguments)
So what will happen is that all events will be ignored in the first frame of existence of the controller, while the event is null, but will be available again starting from the second frame. Is this matching the unexpected result you mentionned, or is it something else?
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#6
(06-01-2020, 11:29 AM)_Aka_ Wrote: What do you mean by unexpected results?
To clarify:
Code:
someEvent?.Invoke(someEventArguments)
is a shorter way to write

Code:
if(someEvent!= null)

someEvent.Invoke(someEventArguments)
So what will happen is that all events will be ignored in the first frame of existence of the controller, while the event is null, but will be available again starting from the second frame. Is this matching the unexpected result you mentionned, or is it something else?

Correct. Above resolves InvokeEventHandler related errors. But I'm still getting errors at Initialize.. should I add ? to it as well? 

From this: onInitialized.Invoke(this);

to this: onInitialized?.Invoke(this);
Reply
#7
yes, do that on all the lines of code that are problematic
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#8
(06-07-2020, 11:14 AM)_Aka_ Wrote: yes, do that on all the lines of code that are problematic

Ok it's good now. Thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Generating GO only on CPs in Generator GameDeveloperek4123 1 6 03-04-2024, 11:06 AM
Last Post: _Aka_
  Missing Loader errors after build FanManPro 8 9 01-20-2023, 11:45 AM
Last Post: _Aka_
  Permission to publish custom CGModule source code? Apelsin 6 9,422 09-15-2022, 08:53 PM
Last Post: Josaf Tom
  Import/Export, Spline from code Mos Def 4 783 02-14-2022, 11:42 AM
Last Post: _Aka_

Forum Jump: