Modify Variable When Selector Is In Range Of NPC

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
User avatar
PayasoPrince
Posts: 104
Joined: Thu Jan 27, 2022 6:47 pm

Modify Variable When Selector Is In Range Of NPC

Post 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? :?:
User avatar
Tony Li
Posts: 21981
Joined: Thu Jul 18, 2013 1:27 pm

Re: Modify Variable When Selector Is In Range Of NPC

Post 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.
User avatar
PayasoPrince
Posts: 104
Joined: Thu Jan 27, 2022 6:47 pm

Re: Modify Variable When Selector Is In Range Of NPC

Post 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? :?:
User avatar
Tony Li
Posts: 21981
Joined: Thu Jul 18, 2013 1:27 pm

Re: Modify Variable When Selector Is In Range Of NPC

Post 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;
}
User avatar
PayasoPrince
Posts: 104
Joined: Thu Jan 27, 2022 6:47 pm

Re: Modify Variable When Selector Is In Range Of NPC

Post by PayasoPrince »

This worked! Thanks! :mrgreen:
User avatar
Tony Li
Posts: 21981
Joined: Thu Jul 18, 2013 1:27 pm

Re: Modify Variable When Selector Is In Range Of NPC

Post by Tony Li »

Happy to help! :-)
Post Reply