change rigidbodies in all objects to kinematic
how do I get all rigidbodies from all objects in the scene and change them to kinematic through c#? tried this, not sure whether its the correct way, but it only changes the one in object that's added last in the scene:
Rigidbody[] rb = Rigidbody.FindObjectsOfType(typeof(Rigidbody)) as Rigidbody[];
foreach(Rigidbody body in rb)
{
body.isKinematic = true;
}
This depends if you want to changed the at game start (that would mean the modification/change would be temporary and would revert when game ends) or if you want to make a permanent change before game starts.
Just by allowing your code to execute in Awake
or Start
will perform this task for you, buf if you desire the latter (make the change permanent) you can go one of two ways - create a $$anonymous$$enu Item
(preferred) or create a button that displays in the inspector.
Good point @$$anonymous$$avina, I totally asumed felsiska meant runtime, but obviously editor time would be useful as well! Good catch ;)
Answer by ThePersister · Dec 02, 2016 at 10:32 AM
Hi @felsiska,
Your code seems to work just fine! https://gyazo.com/9c98f42c68342c939d6dbf0aa157ff63
The only thing I can imagine is that you call it too soon, or you have other code that prevents or influences the isKinematic variable in a different way.
Try using it in an empty scene and you'll see it works.
Try to:
Find other code that edits the isKinematic variable.
Try to set a different set of rigidbodies that aren't used anywhere else.
Debug the state of isKinematic, does it get set to true, and what is the name of that rigidbody's object.
See if you can find out more, but you're on the right track!
I hope that helps, if it does, please accept this answer, it'd be much appreciated!
If you need any more details or if you find out more with which I can help, let me know! :)
.
Best of luck!
Cheers,
ThePersister
thx a lot! I tried on empty scene and works. will double check on other overlapping commands. does Object.FindObjectOfType only works on one object in the scene?
Yes it does! $$anonymous$$ake sure to use:
FindObject*s*OfType ins$$anonymous$$d of FindObjectOfType.
If you want multiple objects, your original post indicated that you were though!
For Single Object: https://docs.unity3d.com/ScriptReference/Object.FindObjectOfType.html
For $$anonymous$$ultiple Objects: https://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html
Note that Fidn object of type doesn't work on inactive objects, so if you have any inactive rigidbodies to update you might want to apply a different method of referencing them! :)
If you need anything else let me know!