I’m pretty new with Unity but I’ve been working on a snowboarding game and have used FindObjectOfType().speed = 40f; etc to modify the speed of the surface effector when I’d like the player too ‘boost’. The boost function works fine with a single surface effector in the scene.
So I added another game object and put a surface effector on that too but now the speed/boost modificaiton doesn’t work even on the original surface that was working before.
So I tried FindObjectsOfType().speed = 40f; with the ‘s’ plural to change the speed of all the surface effectors in the scene. I also tried FindObjectOfTypeAll().speed = 40f; with the hope that would work.
I know FindObjectOfType finds the first object (surface effector) of that type (in the heirarchy order, it think) so I thought FindObjectsOfType or FindObjectsOfTypeAll would find all those components and I would be able to modify all the speeds of the surface effectors with the same command.
I did not expect that adding another surface effector would change the way the first one would operate.
I’ve tried searching for tuturials or forum posts on surface effectors but haven’t found anything about changing the speed with multiple game objects that use a surface effector in a scene.
Thanks for the help!
But this isn’t a 2D question nor is it a SurfaceEffector2D question really; it’s a scripting question on how to access objects in arrays.
FindObjectsOfType returns an array. An array doesn’t have a “speed” property so what you’re saying above wouldn’t compile. You’d need to iterate the array and manipulate each object. I have no idea what you mean by your comment on using 's for plural.
Also, calling this is very slow as the docs state; if you’ve not added/removed these object types then you’re best to store the array somewhere for future reference.
Maybe you should ellaborate or post a snippet of real code using code-tags. Using code tags properly
1 Like
As I said, this call is very slow so you shouldn’t be doing it very often but this is one way to quickly iterate an array and manipulate the objects it refers to:
foreach(var effector in FindObjectsOfType<SurfaceEffector2D>())
{
effector.speed = 40f;
}
1 Like
I’ve not used arrays much to this point. I would guess I’d need to create an array of surface effectors prior to the speed modifying function and then access the appropriate array element to then change the speed.
I think I’m beyond my current skills right now.
If I could find an example of how to use FindObjectsOfType I’d like to think I could figure out how to do something similar but sadly haven’t been able to find anything.
That’s fantastic MelvMay! Thankyou, I’ll try that.