0
Answered

How to change variable value of component for all instances at once

martin.matulik 4 months ago updated 3 months ago 2

1) I have a custom script ("NPC motor") as a component on multiple object. During runtime, I need to change value of custom variable ("speed") of this custom script - for all objects with this component. What is the most effective way to achieve this using BOLT?

2) I have created bool parameter in ANIMATION-CONTROLLER for triggering transition to other state (by conditioning: parameter false -> true). How can I control this parameter via BOLT? (Animator.setBool does not accept animationController - only "instance"/component "animator" from specific object. The catch is: animationController drives many objects, so I would have to set bool for 30 animators manually).

I have  manually created like 30 variables and wired it manually this time, but will be solving similar tasks in near future, so wondering for proper & efficient way.

Looking forward for Your ideas,
Martin

Bolt Version:
1.4.4
Unity Version:
2019.1.8f1
Platform(s):
Scripting Backend:
.NET Version (API Compatibility Level):
Answered

Hi Martin,

1) You might want to make "Speed" an application variable. These are global and shared. This way, only changing one variable will affect every motor. Otherwise, you would have to use a ForEach loop and FindObjectsOfType to look for all the FlowMachines with the Speed variable, and change it on each one of them. 

2) That is just how Unity works unfortunately; each animator has separate parameters, even if they are defined by the same animator controller. In that sense, you would have to use the same alternate strategy proposed for 1).

+1

Hi Lazlo,

Thank You for a response.

I have actually figured out an elegant solution for (2): Created EmptyObject with box collider ("ActTrig" encompassing all NPCs in a scene). When "ActTrig" activated, this script selects all the NPCs and sets true "Panic" variable for each individual AnimationController. Works like a miracle!


Hope this will help some people to save a lot of time!

Martin