Unity Object to Interface automatic conversion

Eric Welwood 4 years ago updated by Lazlo Bonin (Lead Developer) 4 years ago 11

How do I use a component as an interface?

I included the interface as a type option and I create a unit for modifying one of it's properties. But I can't find what unit to use for obtaining this interface from the GameObject. In code I would use GetComponent<InterfaceName>() but the GetComponent unit output does not attach to the input.

Bolt Version:
Unity Version:
Scripting Backend:
.NET Version (API Compatibility Level):

I actually remade all those GetComponent methods with the API to output to object because of this, I dunno why the hassle, but if you want them you can have them. A generic object can attach to interfaces, so there is a faster solution, just make this Super Unit Macro:

You just attach the output Component from GetComponent to 'To Object' input, and then the output to your interface. Storing a component into a variable and using a get variable also outputs an object which is attachable to everything. It will cast to the type properly.


Not sure why that super unit would be needed?

Bolt will automatically convert from any game object to any of its component and vice versa. Basically, the connection itself takes care of the GetComponent<>() call. You only need a node on the right side which calls methods from this component.

Try adding a IMyInterface as the components type your getting. Now connect that component to the input that takes IMyInterface. Doesn't connect. Not allowed. Which would prevent you from being able to anonymously know the type by contract. Only way it worked was to convert to object first. And letting it cast back from being generic. This just purely pertains to interfaces. Everything else works fine like you said.

Nevermind my previous comment.

Can you screenshot what you're explaining (the connection that can't be made)? Not exactly sure I understand.

Pending Review

Oh, I see. I'll put this under review, maybe I can find some way to fix that.

Thanks for the workaround

The workaround will let me connect the nodes but it throws a runtime error 
InvalidConversionException: Cannot convert from 'UnityEngine.GameObject' to 'ISessionMenu'.
Ludiq.ConversionUtility.Convert (System.Object value, System.Type type, ConversionType conversionType) (at C:/Users/Lazlo/Projects/Ludiq/Ludiq.Core/Runtime/Reflection/ConversionUtility.cs:466)
Ludiq.ConversionUtility.Convert (System.Object value, System.Type type) (at C:/Users/Lazlo/Projects/Ludiq/Ludiq.Core/Runtime/Reflection/ConversionUtility.cs:215)
Bolt.XUnitValuePort.GetValue (IUnitValuePort gettable, Ludiq.Recursion recursion, System.Type type)
Bolt.MemberUnit.UpdateTarget (Ludiq.Recursion recursion)
Bolt.InvokeMember.Enter (Bolt.Flow flow)
Bolt.ControlInput.Trigger (Bolt.Flow flow)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.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:232)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115)
MessengerExtensions.MessengerThatIncludesInactiveElements.InvokeIfExistsSafe (System.Object objectToCheck, System.String methodName, System.Object pParameter)
MessengerExtensions.MessengerThatIncludesInactiveElements.SendMessageSafe (UnityEngine.GameObject gameobject, System.String methodName, System.Object parameter)
UICamera.Notify (UnityEngine.GameObject go, System.String funcName, System.Object obj) (at Assets/NGUI/Scripts/UI/UICamera.cs:1895)
UICamera.ProcessRelease (Boolean isMouse, Single drag) (at Assets/NGUI/Scripts/UI/UICamera.cs:2784)
UICamera.ProcessTouch (Boolean pressed, Boolean released) (at Assets/NGUI/Scripts/UI/UICamera.cs:2834)
UICamera.ProcessMouse () (at Assets/NGUI/Scripts/UI/UICamera.cs:2257)
UICamera.ProcessTouches () (at Assets/NGUI/Scripts/UI/UICamera.cs:2371)
UICamera.ProcessEvents () (at Assets/NGUI/Scripts/UI/UICamera.cs:2071)
UICamera.Update () (at Assets/NGUI/Scripts/UI/UICamera.cs:2030)
A cast should be possible as myGameObject.GetComponent<IMyInterface>() will work in code.