Page 1 of 1

Modify Variable When Selector Is In Range Of NPC

Posted: Fri Jan 28, 2022 9:55 am
by PayasoPrince
Hello,

My PlayController has a bool "IsInRangeOfNPC". I would like to set this bool to true when my PlayerController is in range of a usable NPC.

What is the best way to accomplish this? :?:

Re: Modify Variable When Selector Is In Range Of NPC

Posted: Fri Jan 28, 2022 10:13 am
by Tony Li
Hi,

The Selector component has OnSelectedUsable() and OnDeselectedUsable() UnityEvents and SelectedUsableObject and DeselectedUsableObject C# events for this purpose.

In your PlayController script, you can hook into the C# events. Example:

Code: Select all

void Start()
{
    Selector selector = GetComponent<Selector>();
    selector.SelectedUsableObject += () => { IsInRangeOfNPC = true; }
    selector.DeselectedUsableObject += () => { IsInRangeOfNPC = false; }
}
Or you can use the UnityEvents if you want to hook something up in the inspector.

Re: Modify Variable When Selector Is In Range Of NPC

Posted: Fri Jan 28, 2022 11:39 am
by PayasoPrince
Hello Tony,

Thank you for always replying so swiftly. I had looked into using the ProximitySelector's OnSelectedUsable() in the inspector. However, when I drag in my CharacterController and select my PlayerController's class, I don't see an option to access any of the variables in the class :?

Image

What can I select to modify the PlayerController's IsInRangeOfNPC bool? :?:

Re: Modify Variable When Selector Is In Range Of NPC

Posted: Fri Jan 28, 2022 1:28 pm
by Tony Li
Hi,

UnityEvents generally don't let you change variable values directly. You can write a C# method in your script and connect the UnityEvents to that. Example:

Code: Select all

public void SetNPCInRange(bool value)
{
    IsInRangeOfNPC = value;
}

Re: Modify Variable When Selector Is In Range Of NPC

Posted: Fri Jan 28, 2022 1:57 pm
by PayasoPrince
This worked! Thanks! :mrgreen:

Re: Modify Variable When Selector Is In Range Of NPC

Posted: Fri Jan 28, 2022 2:05 pm
by Tony Li
Happy to help! :-)