Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting MissingReferenceException from the component pool when I destroy a spline
#1
I'm creating & destroying splines at runtime using the following code:


Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FluffyUnderware.Curvy;

public class BranchMesh : MonoBehaviour
{
    public CurvySpline spline;

    public Branch branch;

    void Start()
    {
        branch = GetComponent<Branch>();
        spline = CurvySpline.Create();
        spline.transform.parent = transform;

        spline.UsePooling = true;
        spline.Interpolation = CurvyInterpolation.CatmullRom;
    }

    public void PopulateSpline()
    {
        foreach (BranchSegment bs in branch.Segments)
        {
            CurvySplineSegment tempspl = spline.Add(bs.Position, Space.World);
        }

        spline.Add(branch.LastSegment.EndPoint, Space.World);
    }

}

This is part of a much larger codebase, where PopulateSpline() is called.  When the objects, including the spline objects, are destroyed, I get a bunch of these errors:

Code:
MissingReferenceException: The object of type 'CurvySplineSegment' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
FluffyUnderware.DevTools.ComponentPool.sendBeforePush (UnityEngine.Component item) (at Assets/Plugins/DevTools/Components/ComponentPool.cs:318)
FluffyUnderware.DevTools.ComponentPool.Push (UnityEngine.Component item) (at Assets/Plugins/DevTools/Components/ComponentPool.cs:217)
FluffyUnderware.Curvy.CurvySpline.OnDestroy () (at Assets/Plugins/Curvy/Base/CurvySpline_private.cs:331)

The workaround for the moment is to change the UsePooling line in the above code to set it to false.  This prevents the error, but I assume it's going to harm performance if I do that.  I'm generating many thousands of spline points.

So is there a graceful way to destroy the splines without generating these errors from the component pool?

Another thing I want to do is to add a shape to each point along the line.  What's the best way to do that from this code?  At the moment they'll just be circles, but I'll be making them more detailed later.
Reply
#2
Hi,

Maybe the instance is destroyed twice? Can you send me a reproduction project. If it is not possible, can you send me the code destroying the CurvySplineSegments?

All shapes are inheriting from CurvyShape, like CSCircle. You can add such classes either to a gameobject (AddComponent<>()) with a spline, or without (in that case the spline script will be created automatically)

To get the position of a control point: spline.ControlPointsList[i].transform.position

Did I miss something?

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
With the destruction, I am doing something a bit different.  I have a vast, complex scene hierarchy on top of a single parent object and the simplest way to destroy it was recursively, so I've got an extension method on GameObject that does that:

Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

internal static class ExtendUnityObjects
{
    /// <summary>
    /// Destroys all child GameObjects recursively.
    /// </summary>
    public static void DestroyRecursively(this GameObject go)
    {
        for (int i = go.transform.childCount - 1; i >= 0; i--)
        {
            go.transform.GetChild(i).gameObject.DestroyRecursively();
        }

        GameObject.Destroy(go);
    }
}

There are other methods in this class, but they don't interact with this method so I haven't included them.  The recursive function only gets called once to reset the geometry, then the objects shouldn't be touched after that by my code.

The error is coming from Assets/Plugins/DevTools/Components/ComponentPool.cs:318, so it's not really my code that's throwing the error, which is why I decided to ask about it.

I should also mention that the DevTools folder is from CurvySplines, I've just moved all my plugins & libraries into a plugin folder.

As for the shapes along the spline, I didn't explain properly, I'm trying to extrude along the spline, but change the size & shape of the extrusion as I go.  I will be roughly circular.  Ideally I'd like to be able to vary the mesh geometry along the length of the spline, including adding noise and adjusting the number of radial segments, but I understand if that's beyond the scope of this plugin.

If I can't vary this stuff within the plugin, I may simply be able to generate the mesh with the splines, then adjust the vertex positions using my own code, but obviously the less I have to do manually the better.
Reply
#4
(11-22-2020, 05:46 AM)Excrubulent Wrote: As for the shapes along the spline, I didn't explain properly, I'm trying to extrude along the spline, but change the size & shape of the extrusion as I go.  I will be roughly circular.  Ideally I'd like to be able to vary the mesh geometry along the length of the spline, including adding noise and adjusting the number of radial segments, but I understand if that's beyond the scope of this plugin.

Changing the size of the extrusion's shape is doable here.
Changing the actual shape of the extrusion is doable with this module. It is used in example scene 27_CGVariableExtrusion


(11-22-2020, 05:46 AM)Excrubulent Wrote: The error is coming from Assets/Plugins/DevTools/Components/ComponentPool.cs:318, so it's not really my code that's throwing the error, which is why I decided to ask about it.
Of course, I am not saying the opposite. But still I have to ask you questions to know how to reproduce the bug in order to fix it. Your use case seems very particular, since I am not aware of any other user having your issue.
After reading your code, I can confirm my earlier guess, which is that you are destroying objects twice. You do not need to destroy the children, since destroying the parent will lead to the destruction of the children: "If obj is a GameObject, it destroys the GameObject, all its components and all transform children of the GameObject" from https://docs.unity3d.com/ScriptReference/Object.Destroy.html
In conclusion, avoiding the destruction of a spline twice will avoid the raise of the exception.
Did this help?

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
#5
Okay, thanks, that's great, I'll take a look at the shapes stuff.

And as for destroying objects twice, that does make sense. For some reason that wasn't working for me, and I can't exactly remember why. I'll have to go back and take a look at whatever it was that made me do that in the first place. I appreciate the information, thanks Smile
Reply
#6
You are welcome
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
  Curvy Line Renderer for UI Spline? gekido 1 1 Yesterday, 10:12 PM
Last Post: _Aka_
  Get position of all control points for a spline gekido 1 2 Yesterday, 10:08 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_
  GO can't fit end of the spline GameDeveloperek4123 3 13 03-04-2024, 11:06 AM
Last Post: _Aka_

Forum Jump: