Unity child transform problem [SOLVED]

edited in Questions and Answers
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

Screenshot.jpg
1920 x 1080 - 297K

Comments

  • edited
    @LittleBear

    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.

    private void RotateBy(float angle)
    {
       transform.Rotate(new Vector(0, 0, angle)); 
    }


    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.)
  • Um. I don't understand what your script is trying to do - could you post the code itself? Also, why are you trying to get pointers to objects - are you trying to make the turret rotate towards a specific thing maybe?

    Can you rotate something at will without a hierarchy?
  • @hermantulleken - thank you so much for those links!!!!! I have also been struggling with 2D rotation in a recent project and this will help a lot.

    @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.
  • edited
    ParentGameObject.transform.GetChild (0).gameObject gets the first index of the children, if that happens to be the base, the base will be retrieved and not the turret (since the turret might be index of 1). only use indexes if your really know what is going on in your structure. for you right now I think it would be better to get the child by a string name.

    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
  • Yeah, what Pixel_Reaper said, get child methods reference the base object at index 0, even though it is the "parent"

    Rather have a direct reference you populate via inspector:

    public GameObject turret;


    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.
  • edited
    Hey guys, wow sorry when rereading my original post I realized it lacked allot of info.

    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:
    private float zRotation = 0.0f;
    
    // Update is called once per frame
    void Update ()
    {
    	zRotation += 1;
    	transform.eulerAngles = new Vector3(0, 0, zRotation);
    }


    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!
  • @littlebear: Try to use transform.rotation = Quaternion.Euler(0, 0, zRotation).
Sign In or Register to comment.