Hi,
I am wondering if there is a function built into Unity that lets you change properties of certain GameObjects without looping through them. Let’s say I have a simple script called “HasSpeed” with just the property speed.
public class HasSpeed : MonoBehaviour
{
public float Speed = 1.0f;
}
In a different script I want to change “Speed” without looping through all of the GameObjects. Here is what I don’t want (There are many different ways to get the objects but in essence I would have to loop through them):
public class ChangeAllObjectsWithSpeed : MonoBehaviour
{
void Update()
{
if(Input.GetKeyDown(KeyCode.W))
{
HasSpeed[] speedScripts = (HasSpeed[]) GameObject.FindObjectsOfType (typeof(HasSpeed));
foreach(var current in speedScripts)
{
current.Speed = 0.5f;
}
}
}
}
I just want to know if there is a Unity built-in way to handle those kind of things. Is there something which I can use to achieve what I want?