- Home /
Disable isKinematic on children
I'm doing a mouse collision with one object and I want to enable isKinematic of his children...
Any idea to do that ? Am I doing Wrong ? This script is on Gameobject that I want to take the children
public class MouseDrag : MonoBehaviour
{
public GameObject fruit;
void Start()
{
fruit = GameObject.Find("Fruta");
}
Vector3 screenPoint;
Vector3 offset;
void OnMouseDown()
{
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnMouseOver()
{
if(transform.gameObject.tag == "Player")
{
print ("worked");
gameObject.GetComponentInChildren<Rigidbody>().isKinematic = false;
//rigidbody.isKinematic = false;
}
}
Answer by Crazydadz · Mar 27, 2014 at 07:04 PM
Cache an array of all the rigidbody of the children with GetComponentsInChildren
Something like:
Rigidbody[] bodys = GetComponentsInChildren<Rigidbody>(); //Or GetComponentsInChildren<Rigidbody>(true) to get inactive children
for(var i=0;i<bodys.Length;i++)
{
bodys[i].isKinematic = false;
}
I havent tested it, but it can give you an idea.
showed this error CS0266: Cannot implicitly convert type UnityEngine.Component[]' to
UnityEngine.Rigidbody[]'. An explicit conversion exists (are you missing a cast?)
Rigidbody[] bodys;
private void Start()
{
bodys = GetComponentsInChildren<Rigidbody>() as Rigidbody[];
}
Answer by ava4414 · Mar 27, 2014 at 07:46 PM
it's easy make this:
Rigidbody[] bodies = gameObject.GetComponentsInChildren<Rigidbody>();
for (int i=0; i < bodies.Length; i++){
bodies[i].isKinematic = true;
}
so ins$$anonymous$$d of "true" you can change it to false
Your answer
![](https://koobas.hobune.stream/wayback/20220613141332im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Function works when called in Start() but wont when called in OnTrigger2D 1 Answer
Find children and set them all to be kinematic rigidbodies 1 Answer
Make UI Children Fit the size of their parent 1 Answer
How to detect OnClick on children of a 3D model 1 Answer
Baking NavMesh at Runtime: Connecting separate NavMeshSurfaces 0 Answers