Cannot get the cleanup of the explosions appearing in the root hierarchy

I have the exact code in there but it always goes into the root.

public class PowerNode : MonoBehaviour {
    [SerializeField] GameObject deathFX;
    [SerializeField] Transform parent;

    // Use this for initialization
    void Start () {
		
	}
    void OnParticleCollision(GameObject other)
    {
        GameObject fx = Instantiate(deathFX, transform.position, Quaternion.identity);
        fx.transform.parent = parent;
        Destroy(gameObject);
    }
    // Update is called once per frame
    void Update () {
		
	}

The power nodes are getting destroyed, but they deathFX never gets sucked up into the empty object transform as expected.

Hi Jens,

To debug, I’d probably start by dropping a couple of Debug.Log statements in to your existing code and then see what the output is;

void OnParticleCollision(GameObject other)
{
    GameObject fx = Instantiate(deathFX, transform.position, Quaternion.identity);

    Debug.Log("Initial parent transform : " + fx.transform.parent.name);    // most likely going to be null
    
    fx.transform.parent = parent;

    Debug.Log("Update parent transform : " + fx.transform.parent.name);     // should be the name of the parent you passed in
    Destroy(gameObject);
}

Incidentally, rather than setting the parent in the additional line of code as you currently are, you could just use one of the other overloaded Instantiate methods, like this;

void OnParticleCollision(GameObject other)
{
    GameObject fx = Instantiate(deathFX, transform.position, Quaternion.identity, parent);
    Destroy(gameObject);
}

Let us know what the output of your Debug.Log statements ends up looking like.

Here is the log when those are in there. Strangely, when these debug logs are in, the object is never destroyed but can be continually hit. Not sure why that is.

image

I am getting this “line ending” warning. could this have something to do with it?

I tired the code with the parent in the instantiate function and it works fine. The objects still do not go into the intended parent, however.

That would be because of the error message, the error prevents further code from executing, which would have been the Destroy(gameObject) statement.

Nope. That’s just because of differences between the code you’ve written locally and I’m guessing what you copy/pasted from my post. We are using different operating systems, the line endings are different.

Those NullReferenceException errors are most likely your key here. If those are being caused by adding those Debug.Log statements, then the thing we are trying to access doesn’t exist.

Which line was line 16?

16 was the first one you provided. The “Initial parent transform”.

Ok, so where I added the comment suggesting that it may come back as null, it did, and of course then we tried to access the name property of a null reference which would make perfect sense to cause that error message.

So, remove that first Debug.Log statement, as we now know that the initial parent is null, e.g. there isn’t one. Then, run the code again and see what the second Debug.Log statement outputs.

New error popped up which I will try and figure out, but the second log is the line 18 error here.

image

Conscious you have a warning there about a missing script, but assuming that isn’t part of this problem… if after setting the parent to the parent variable you are getting a NullReferenceExecption error then something in this line;

fx.transform.parent.name

has to equal null, most likely, the parent because we are trying to access the name property.

So, lets test that…

Remove that second Debug.Log statement completely and replace it with this;

if(parent == null)
{
    Debug.Log("parent is 'null'");
}
else
{
    Debug.Log("parent is not 'null'");
}

Run the game and see which of those two messages you see.

I will do that in one second, but I shot 1 of them and got this message.

image

So, one of them worked correctly, which leads me to a possible prefab error, but I did check the instances for the scripts and parent name. Going to double check the assets and insert this code.

One thing you could do which may make it easier to see is to Pause the game before Playing it, then click Play, the game will start paused, you can then select your GameObjects where you’ve made the reference to the Spawned at Runtime GameObject and see if it’s actually there.

I have some other theories too but lets see where this takes us first.

Okay. I think I have resolved the issue with your help to my satisfaction. In short, this was not a code problem or any hidden syntax snafu in my script. It is an asset error in the scene. The parent was not set up so I added it to several instances. Now I am getting other weirdness happening with double objects. So, I am going to simply delete and recreate these items and retest. That should tidy everything up.

I thought I was being extremely diligent by applying any work I did on a prefabbed object to the prefab. At some point, I must have broken and improperly re-linked something. Thank you for the help thus far as it made me realize something in unity at the very least. :slight_smile:

1 Like

Great to hear you’ve made some progress with this Jens and you are more than welcome.

My other theories were along the lines of either the Spawned at Runtime GameObject not being set on a specific prefab instance, or, possibly having duplicated scripts where in one the object reference was created and in the other it wasn’t. Depending on the script execution order, it could have been possible for the one without the reference to override what had been set in the one that was.

Anyway, good luck with your project and I hope you enjoy the rest of the course :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms