Child gameObject doesn't move with the parent gameObject - unity3d

So, right now I'm working on a Unity game where if you click on an item, it will become the child of the click object. The click object is also the child of the player and moves with the player, but the object that gets clicked (making it the child of the click object) just stays where it is. Even if you move. I don't know why it doesn't move with the player, does anyone know why?

Never nest (parent) Rigidbody2D (=> physics based objects) under another object.
It doesn't behave as you expect since the Physics engine doesn't care about the hierarchy of the objects.
If you need a rigidbody to follow another object rather use Joints!

Related

Can you attach a sprite renderer to a child of a main game object and still have the animator access functions from the main object for events?

When creating a player or enemy in unity, I've learnt that it's best practise to attach the sprite renderer to a child game object so that you can easily reposition the sprite. However, doing this means that the animator also needs to be attached to the child game object, which means you don't have any access to the functions from the parent game object for animation events . Does anyone know a way around this?
I tried putting the animator on the parent object, but then it said the sprite renderer component was missing so I believe they need to be on the same object.
I tried putting the animator on the parent object, but then it said the sprite renderer component was missing so I believe they need to be on the same object.
No! What happened is you moved the animator root so the original property paths stored in your animation clip is invalid now.
The property paths are basically stored via the objects names.
What previously was e.g. (don't know the exact form of the property paths)
/Sprite/SomeProperty
would now need to be something like
YourSpriteObject/Sprite/SomeProperty
but since the animation still uses the first one and there is no Sprite component on your parent object it is invalid and displays as missing.
You will have to redo your animations or write an editor script for automatically changing all animation paths accordingly.
Here I once did something similar for renaming animated objects. You could probably write a similar thing for moving the animator component to a different object in the hierarchy
Alternatively expose the methods on your child object, and then forward them to trigger stuff on the parent via
transform.parent.GetComponent<YourParentClass>().YourMethod();
which of course I would either cache once or assign via the Inspector

How to prefab animations in Unity?

created a prefab for door and added the prefab doors to the scene. This prefab door has an animator. The problem is when I try to change the animator x,y,z position on the second door in the scene it changes the first door.
I tried duplicating the doors and changing the position x y z but it changed the animator for the first door.
I just have a script that play the animator.
The expected result should be that I can drag and drop doors as needed without creating individual animators for each door. It looks like i need to create animators per door there is and also this probably will require me to create new scripts per each door. It seems awkward since I should be able to add doors and just chaning the position x y z in the animator.
The problem is that their Animator component uses the same AnimatorController asset which uses certain AnimationClip asset references for it's states.
I don't know if there is an easier way for achieve but I would do the following:
For what you want you would have to copy the AnimatorController asset (CTRL+D) as well as the AnimationClip asset you want to change.
Then in the cloned AnimatorController click on the according State you want to change and in the Inspector reference the according cloned AnimationClip.
Now you have a copy of the AnimatorController using a different AnimationClip but with the same states, traditions etc. so you can edit both animations and controllers individually.
finally in the Animator component reference the according AnimatorController for each prefab.
The remaining problem maybe is now that for the future if you change the States, transitions and especially parameters you have to do it in both AnimatorController assets individually.
That is perhaps not because of an animator, but a specific behavior of animation in Unity. Animation animates transform property according to parent, as far as I understand. So the first parent of your prefab is captured, and its transform property is used for animation. But I'm not exactly sure about this explanation - the actual behavior is covered under the Unity's source code. A common solution for this that I found is to create a parent or container inside your object's prefab. I don't know why it works, but it works. Perhaps because the parent of your animated object is now always the same.
Parent can be just an empty game object. So you make your door prefab, place another empty game object into this prefab, make your door a child of this container, set 0 for all coordinates of your door according to container and for the container itself. Now you can add an animator component to your object, which you want to animate (not the container), and animate it inside the prefab scene. After that you can place the copies of this prefab (container + your animated object inside) wherever you want inside your game scene - all animations of copies will proceed independently.

How can you make a prefab that can expand to show its constituent objects?

I am very new to Unity3d. I want to make an prefab that expands with a little right facing button to show other objects. I have tried giving the prefab children and then dragging it into the project window, but it didn't show the little right-facing arrow. Can anybody help?
To add components to a prefab, add it to the scene as you said you have, then drag your next object on top of the prefab in the Hierarchy window. Sometimes this places it relative to the prefab but does not actually child it, so simply drag the object now in your Hierarchy onto your prefab once more and it should fall in place as a child, you will know this worked if you now have an expand node next to your prefab.
First, make your root game object that you will convert into a prefab.
In order to add objects which are children, right click on the root object and then make the children. You can also drag other gameobjects onto the root game object to make them children of the root game object.
Finally, drag your final prefab into window where all of your files are, and the resulting prefab will have children.
If you want to change the prefab after it is made, you have to change one object that results from your prefab and after changing it, click Apply in the top of the Inspector window to apply that change to all other objects resulting from the same prefab.

How to check contact between two game objects with one having child game objects that change position

I want to check contact between one game object (ObjA which has a box collider and a rigid body) and another game object (ObjB) which has two child game objects. The child objects of ObjB change positions slightly. I haven't set a box collider for ObjB because I don't need it to react to physics, I just need to tell when one object (ObjA) is in contact/touches it. How can this be done?
You have to add a box collider to ObjB and check the boolean "Is Trigger". This will still allow things to go through it and not collide or bounce off.
However, now instead of onCollision enter, you need to use onTriggerEnter as done here:
http://answers.unity3d.com/questions/581977/how-to-get-collision-point-when-using-ontriggerent.html

Can't apply prefab changes

i have a problem when try to apply changes.
I have a GameObject in the hierarchy, i put other GO in a custom script and push apply button. All has saved, but 2 game object can't apply canges.
Thanks in advance.
If you add new objects to the transform hierarchy, those new objects can't reference the prefab because they don't know they're part of the prefab. You can tell this because in the screen hierarchy list the prefab items are blue and the rest are not. You have to save the changes from an object that is part of the prefab first or redefine the prefab by dragging the parent object onto the prefab on the project list. Because a direct save was possible, it will overwrite.
The same is still essentially true for removing objects from the prefab hierarchy, except Unity knows that this is a breaking change (because it wouldn't be mappable to the prefab anymore) and informs you that doing so well remove ALL of the objects from the prefab upward reference and you can only save the changes by redefining the prefab by dragging it from scene to project (and possibly receive another warning about the objects being different: are you sure you want to overwrite?).
Likely those Text and Transform objects are found outside of the prefab. Prefabs can't reference external objects, since there's no guarantee they'll be available for every instantiation of the prefab.