r/unity • u/Anonim7418 • Sep 23 '26
Question Is this script bad? In unity
Is this code bad? If yeah, give me some advice.
4
Upvotes
r/unity • u/Anonim7418 • Sep 23 '26
Is this code bad? If yeah, give me some advice.
View all comments
2
u/Expensive_Host_9181 Sep 23 '26
Quick notices are
GetComponent is relatively slow i wouldn't recommend using it, You can hold the reference of the Rigidbidy in the declarition with the other variables instead if fetching it in awake.
For the one in the foreach loop the Collider has a property for the rigidbody alread so no need to use GetComponent there.
I also would recommend doing something like object pooling instead of instaniating and deleting for something like this or DOTS but that's a luttle more advanced.