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?
Modify Variable When Selector Is In Range Of NPC
- PayasoPrince
- Posts: 104
- Joined: Thu Jan 27, 2022 6:47 pm
Re: Modify Variable When Selector Is In Range Of NPC
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:
Or you can use the UnityEvents if you want to hook something up in the inspector.
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; }
}
- PayasoPrince
- Posts: 104
- Joined: Thu Jan 27, 2022 6:47 pm
Re: Modify Variable When Selector Is In Range Of NPC
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
What can I select to modify the PlayerController's IsInRangeOfNPC bool?
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
What can I select to modify the PlayerController's IsInRangeOfNPC bool?
Re: Modify Variable When Selector Is In Range Of NPC
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:
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;
}
- PayasoPrince
- Posts: 104
- Joined: Thu Jan 27, 2022 6:47 pm
Re: Modify Variable When Selector Is In Range Of NPC
This worked! Thanks!
Re: Modify Variable When Selector Is In Range Of NPC
Happy to help!