How to get child object child gameobject in unity? - unity3d

I have one doubt, how to get Child object child. I have one parent Gameobject name bed_1 that parent have four child object in that four child object one child have another 5 child how to get that 5 child!
Detailed Image:

transform.GetChild(3).GetChild(4)
If you do this, I recommend null-checking.
See https://docs.unity3d.com/ScriptReference/Transform.GetChild.html
However, I could recommend you to create a variable:
public Transform TargetChild5;
And then assign the value by dragging the 5th child into the slot in your script (in inspector)

Related

unity how do I get a component from another script?

I have an animator component on one object and I have to access it in one of my scripts. But I do not know how to do it. Of course, it could be done via SerializeField, but I can't do that, since I can't select the animator of another prefab for my current prefab (since it's not visible to it). So it remains to do this only through code, if possible. The access modifier for the animator has already been configured (public).
I assume you have a GameObject A with an animator component, and a GameObject B with a custom script that needs a reference to an animator component.
If both are in the scene, or if A is a prefab:
Select GameObject B, then drag GameObject A into the reference slot of your custom script. If A is a prefab you can call Instantiate(animatorOnA) and it will return a reference to the animator on the newly instantiated object.
If both are instantiated in the same script, you can use GetComponent() on the instantiated copy of A's prefab to get a reference to the animator. Then you can assign the animatorOnA field on B using this reference.
If A is a child of B then you can assign it directly in the inspector, and it will automatically update the reference.
So I assume at one point you instantiate the prefabs in the scene, I don't know which one you instantiate first but you could do this in the script that instantiates those prefabs like this:
// The instantiate function will return the object you instantiated so you can store that in a variable
var prefabWithTheAnimator = Instantiate(yourPrefabWithAnimator);
var prefabThatNeedsTheAnimator = Instantiate(yourOtherPrefab);
// Now you can get the animator of the prefab that has it and set it to the variable in the script you need
prefabThatNeedsTheAnimator.GetComponent<TheScriptWhereYouNeedTheAnimator>().animatorRequired = prefabWithTheAnimator.GetComponent<Animator>();

SpriteMask in Unity

There are many identical objects, each of which has a child.
The child object must not appear outside of the parent. I do this using SpriteMask, but with this approach, the child object sees within the boundaries of someone else's parent, as in the screenshot

How can i reset positions of Prefab children's in unity 2d. children's are also prefabs

I have an object which is prefabs and that object contains further objects(assets) which are also prefabs. I want to Reset position of Parent object and Children object when level fails. however I am not able to reset position of children object by instantiating. I can instantiate parent object but can not instantiate children objects. when I reload the scene, Scene get reloaded with the object which was active when game started not with the object on which level failed.
for reset all children's transform you can get it by GetComponentsInChildren<> like this:
Transform[] childList=parentObj.GetComponentsInChildren<Transform>();
foreach(var i in childList)
{
i.localPosition = Vector3.zero;
}
you can call it after instantiating prefab or after restarting game.
another way is attach an script to each children of prefab and in awake or start function reset the position. if it should not rest by zero you can store your default transform and set it on awake or start function.

Object is not clickable when it is a child of another object in unity

So, i made a simple code in unity that outputs "clicked" when the object is clicked by using this simple code :
private void OnMouseDown()
{
Debug.Log("clicked");
}
it displays the message when clicked, but when the same object is a child of another object it is not clickable and doesn't display the message.
How do i make it clickable when it is a child ?
You should always be aware of the colliders of parented objects. When the collider of your child object is within the collider of the parent object the OnMouseDown trigger will only hit the first collider it (well) collides with.
You need the collider of the child object to be reachable.
You can deativate the collider of the parent object.
You can scale the collider of the child object up.
Or you write your own method, which only checks colliders with a given tag.
Just add a Rigidbody to the parent object (probably in your case you also need to set "Is Kinematic" to true) and make sure child objects have colliders.

how to parent follow children in unity

I make an empty game object and added sprite render component. Then because of inconsistency sprite renderer and NavmeshAgent I was forced to create one Emptygameobject as child of parent EmptyGameobject then add NavmeshAgent
component.in runtime. I need parent Gameobject follow childgameobject.
I write a class for navmesh move and I attribute it to objects by drag and drop it. look like this :
All the objects in this class are moving.
Following the child object is not done correctly.
that move class called in the following form
why would you want to move the child with the parent following, you can just move the parent. the child will follow and there will be no difference in functionality