Unity child transform problem [SOLVED]
I created a hierarchy with the Platform of the turret as the parent object which also holds the script, I am struggling to rotate the Turret child object and the following code does not work:
GameObject ParentGameObject = GameObject.FindGameObjectWithTag("Tag1");
GameObject ChildGameObject0 = ParentGameObject.transform.GetChild (0).gameObject;
If I put the rotation script on the turret object the base rotates and the turret stays still, son of a b**** :(
Any help would be greatly appreciated, just started unity a few weeks ago.
Ps. I have googled alot and can't find a solution and I'm using unity new 2d features
![image](http://www.makegamessa.com/uploads/FileUpload/9c/e2e0731205c70d691858d690918a28.jpg)
GameObject ParentGameObject = GameObject.FindGameObjectWithTag("Tag1");
GameObject ChildGameObject0 = ParentGameObject.transform.GetChild (0).gameObject;
If I put the rotation script on the turret object the base rotates and the turret stays still, son of a b**** :(
Any help would be greatly appreciated, just started unity a few weeks ago.
Ps. I have googled alot and can't find a solution and I'm using unity new 2d features
![image](http://www.makegamessa.com/uploads/FileUpload/9c/e2e0731205c70d691858d690918a28.jpg)
![](https://makegamessa.com/uploads/thumbnails/FileUpload/9c/e2e0731205c70d691858d690918a28.jpg)
![](https://makegamessa.com/uploads/thumbnails/FileUpload/9c/e2e0731205c70d691858d690918a28.jpg)
Screenshot.jpg
1920 x 1080 - 297K
Comments
Post your complete rotation script so that we can see what's up.
Edit:
The following should work if I understand you correctly, with the script on the Turret. Just call the method from where you wanted to call your own rotation code.
This simply rotates the attached object around the z-axis. You need not bother with parents and children if you attach the script to the thing you want to rotate. (It will rotate children with, though).
(PS. Check out our free extensions package; it has a few extensions methods for transforms that makes 2D a bit less painful, such as a RotateAroundZ, SetXY, SetScaleXY.)
Can you rotate something at will without a hierarchy?
@littlebear - please do post your code. As mentioned I have been struggling with something similar so I think any advice given to you, would benefit me too.
This is a response to what I think your problem is, of course, I might be wrong though. :P
PS: ignore this, I didn't look at your picture
Rather have a direct reference you populate via inspector:
Another way you could work around this, if there is a custom script attached to the child but nothing else, GetComponentInChildren<YourCustomComponentType>() is a good way to go.
I'm trying to build a simple tower defense game, the turret must track the enemy but for now I just want to get the turret rotating, the code is simple here it is:
I know that if you drag the script onto the GameObject that that object will rotate, but when I drag it onto the Turret object that is a child of the Platform object the platform rotates instead, while writing this I think I realized how to fix this problem, wil test and let you guys know... :)
Edit:
Ok my idea was to remove the Turret object from the parent Platform object and place them both into a Parent Lvl1_MachineGun_Turret object, did that but when I re drag the rotation script onto the Turret object the platform still rotates instead of the turret..... mother f*****
Edit: Edit:
Deleted the sprites and re dragged them into the Lvl1_MachineGun_Turret object and re dragged the script onto the turret object and now it works.... why? Heck if I know.
Thanks for the help guys!