Something you’ll likely want to do quite often when you’re prototyping or developing in Unity is manipulating the value of a component property on a GameObject, for instance, setting the alpha value of a material’s colour.
With PlayMaker, it’s a very straightforward task when the object you want to affect is already in the scene, but it becomes slightly more complicated when you want to work with a prefab instantiated at run-time. It’s something I’ve stumbled over a number of times in PlayMaker so I've written a quick guide, illustrated with a feature prototype I put together this week.
This prototype was intended to test out the idea of numbers that appear when damage is dealt to a component on the board (ie., a weapon, utility, etc.). It’s a pretty basic idea, but our screen is already quite busy, so readability was one aspect we wanted to test. And of course, as explained in my Unite 2016 talk, most of the PlayMaker work I’ve done in this prototype can be transferred directly to the proper game when the time comes to implement this feature. Essentially what I set out to do was spawn a number above the damaged component, then fade the number out and have it move off-screen, with all the juiciness you'd expect.
As I said above, when you’ve got the object in the scene, it’s very simple. Use the Set Property action, and drag the component you want to access directly from the Inspector into the Target Object variable on the action. You can then set the value for that property.
In my case, I’m setting the alpha, and I’m doing this every frame in conjunction with the Lean Tween Value action, to lerp it between its initial value (obtained using Get Property and the same drag/drop method in the previous action) and 0.
It gets trickier when you don’t have the object in the scene, because the object doesn’t exist when you enter run-time, so how do you get the reference to the component? This example illustrates how to manipulate the alpha value of the material in the MeshRenderer component, but the exact same process can be used for essentially any property of any component.
Using Get Component, Get Property & Set Property with Instantiated Prefabs in Playmaker
Set up your Create Object action and store the object.
Next, create a variable of type Object. Call it something appropriate, and then use the ObjectType drop-down to navigate to UnityEngine> MeshRenderer. Most of the default Unity component types you’ll want to access are in this menu.
Using the Get Component action, specify the GameObject variable you’re creating, and store the component using the 'MeshRendererComponent' variable you just set up.
Now when you want to modify that property, you can use the Set Property action and specify the MeshRenderer variable as the TargetObject, then access any property of the stored component from the drop-down.
You can also use the Get Property action in the same way, to get and store the value of a given Component property. In my example, I’m using Get Property to store the initial value of the alpha, and then Set Property to set the value every frame to that output by the tween.