How to get assigned gameobject of an UnityEvent PersistentTarget?

  1. when i debug.log GetPersistentTarget(i) it gives me the gameobject name too so i think there is an access to its gameobject. i want to check getcomponent of assigned gameobject.
    image

1 Answer

1

GetPersistentTarget returns a UnityEngine.Object. You can covert this to a GameObject by using the ‘as’ keyword, or by casting to a GameObject:

UnityEngine.Object unityObject = unityEvent.GetPersistentTarget (0);

GameObject go = unityObject as GameObject;
//or....
GameObject go = (GameObject) unityObject 

i've tried this but it gives me null error: InvalidCastException: Specified cast is not valid.

Can you post the code you're tying?

After the first line of code above, add: Debug.Log ("object type: " + unityObject.GetType()); What does the console say?

thanks it solved. null error was for my own code.