Functions with Vector3 argument not showing up in UnityEvent

I need a function that will save the Vector3 data to a script variable that can be called from a UnityEvent but it doesn’t show up. I really need a solution

Version: 2021.3.14f1
Code:

public void test(Vector3 pos)
{
    newPos = pos;
}

Thank you if you find a solution

public static TestEvent testEvent = new TestEvent();
public class TestEvent : UnityEvent<Vector3>{}

private void OnDisable()
{
    testEvent.RemoveListener(OnTestEvent);
}
private void OnEnable()
{
    testEvent.AddListener(OnTestEvent);
}

private void SomeFunction()
{
    testEvent.Invoke(new Vector3(1, 2, 3);
}
private void OnTestEvent(Vector3 _vector)
{
    Debug.Log(_vector);
}

little bit of jargon. Its really rather simple, unless im missing something in your problem.

Thanks, but I meant in the editor

i dont follow. What in editor? Whats not in the editor?

public TestEvent testEvent = new TestEvent();
[System.Serializable]
public class TestEvent : UnityEvent<Vector3>{}

that will make an event show in in the inspector, if thats what you mean? I dont think i get what you mean at all.

I mean in this
8875671--1212210--upload_2023-3-14_17-32-18.png

The vector 3 will be passed through as dynamic. as long as you provide a function that indeed passed a vector3.

You want the vector3 to be showing in the inspector?

Yes, as argument of function

You may expect to provide a static vector3 value to your otherwise parameterless event? That’s not possible since UnityEvents only support very basic values types or UnityEngine.Object references as static parameters. The only values that are supported as static arguments are those supported by the ArgumentCache class of the UnityEvent system. Specifically: a single int, float, string, bool or UnityEngine.Object value. Those are the only values you can statically specify inside the inspector for a single staticly provided argument.

When the actual event type accepts dynamic arguments, it would support any kind of argument type and up to 4?! arguments. Though in that case you can not staticly choose values passed to the callback / method in the inspector. Instead the caller of the event has to pass the arguments along.

1 Like

This is sad, I really need this, is there an alternative?

I dont understand the issue here. If the value you need to be passed through is static, just set teh variable inside the script somewhere, if not, then take advantage of the dynamic method / value.

Whats the niche case you are trying to resolve?

1 Like

It depends on your specific usecase. You can specify the argument as a separate variable next to the event and pass it along when you actually call the event. What’s your exact usecase here? What is that vector used for?

Exactly my thought :slight_smile:

I making point & click game and I want to implement interaction

Unfortunately, i think we would need a little more context to assist in this situtation. For the most part, the ins and outs of the event have been covered. Whats missing is the exact useage and expected outcome.

You can always create small delegate components. That’s not that clean but would work just fine.

public class StaticVector3Delegate : MonoBehaviour
{
    public UnityEvent<Vector3> myEvent;
    public Vector3 argument;
    public void Run()
    {
        myEvent(argument);
    }
}

Not tested, just written from scratch. Though you should get the idea. You can attach this component either to the receiver or the sender, hook up your Vector3 methods in myEvent and have the actual event call the Run method. You can specify the value you want to pass along in the “argument” value of that component.

1 Like

But how dirty would it be to hard-code a Vector3 value in an event call like that? What would that Vector3 value actually represent?

I need to call UnityEvent when the player clicks on an object

This is new position for player

Then, how could a hard coded value be of any use? I really think you would want to use dynamics, but again, maybe im still missing a large piece of the pie. :stuck_out_tongue:

To move player I want use Vector3.lerp function