ORIO Portection

When returning to my Argon Assaut project today, Unity had lost the reference to the parent for my enemy deathFX and wouldn’t let me reset it. So I came up with this script to add to the EnemyController

using UnityEngine;

        private void SetParent()
        {
            if (parent = null)
            {
                parent = GameObject.Find("Spawned at Runtime").transform;
                if (parent = null)
                {
                    parent = new GameObject("Spawned at Runtime").transform;
                }
            }
        }

but now I’m wondering if this is an acceptable way to protect against an ORIO (Object Reference not set to the Instance of an Object)?

Doing this statement to check if the objects name is not null

That’s the primary reason

You’ll get a NullReferenceException anyway if GameObject.Find returns null. A proper solution would be:

        private void SetParent()
        {
            if (parent != null) return; // terminate method

            GameObject newParent = GameObject.Find("Spawned at Runtime");

            if (newParent != null)
            {
                parent = newParent.transform;
            }
            else
            {
                parent = new GameObject("Spawned at Runtime").transform;
            }
        }

See also:

Thank You Nina,
That looks better than what I wrote

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

Privacy & Terms