- Home /
How to unparent a Transform by UnityEvent?
I want to clear the parent of the referenced transform in the UnityEvent, but assigning Transform -> SetParent() or Transform -> parent as function to call and leaving the parameter to null causes an ArgumentException:
ArgumentException: failed to convert parameters
System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:484)
System.Reflection.MonoCMethod.Invoke (BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:528)
System.Reflection.ConstructorInfo.Invoke (System.Object[] parameters) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/ConstructorInfo.cs:77)
UnityEngine.Events.PersistentCall.GetObjectCall (UnityEngine.Object target, System.Reflection.MethodInfo method, UnityEngine.Events.ArgumentCache arguments) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:429)
UnityEngine.Events.PersistentCall.GetRuntimeCall (UnityEngine.Events.UnityEventBase theEvent) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:396)
UnityEngine.Events.PersistentCallGroup.Initialize (UnityEngine.Events.InvokableCallList invokableList, UnityEngine.Events.UnityEventBase unityEventBase) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:569)
UnityEngine.Events.UnityEventBase.RebuildPersistentCallsIfNeeded () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:736)
UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:772)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:52)
This is on a prefab though, so setting the parent to an other transform far away is not possible. Is there any way to without scripting this? I dont want to create a new script just for this. Using 2018.2
Answer by DenisIsDenis · Jun 30, 2021 at 04:01 AM
It looks like the simplest and most suitable solution for you is to create a script. But I would still suggest that you create an empty object, put an object in the zero coordinate and then SetParent () to it.
Script (in one line :) ):
using UnityEngine;public class SPTN:MonoBehaviour{public void SetParentToNull(){transform.SetParent(null);}}
Assign this script to the object that should be unparented and add this object to the button and select SetParentToNull()
from the list of functions.
But suddenly it helps you: the Transform
has a DetachChildren()
function, which is unparentalized to all child objects.
Your answer
![](https://koobas.hobune.stream/wayback/20220613035625im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why are my (null) scripts receiving events? 1 Answer
Why am I getting a null error? 1 Answer
Parenting from code doesn't really change the hierarchy 1 Answer
NullReferenceException with SwitchController script 1 Answer
How does Transform.SetParent() worldPositionStays work with null parent? 1 Answer