Parenting and Scripting

I’m following a old guide/tutorial for Unity3D (in which it still believes the scripting program used is UniSciTE). While old, I’ve been following along well enough to understand the content and place my foot in the door for Unity.

In any case, I’ve come to a brick wall. I’m able to climb the wall, but I’m doing so blindly.

The book as just introduced me to Scripts and basic parenting (I say basic because the book does not explain the actual feature until later into the book). We’ve just passed the bases of learning basic transformation tools. It explains the way the engine reads the script and why two transformation functions cannot be used in one script at the same time. So it has me create an empty (which I name Cube Parent) and attach a script (one used for moving) to it, while the script for rotating is attached to the actual object (which in this case is a cube simply named “Cube”).

The code for the moving is as follows:

    #pragma strict

function Start () {

}

function Update () {


//transform.Rotate(0,50 *Time.deltaTime,0);
transform.Translate(0,2 *Time.deltaTime,0);

}

The code for the rotate is similar, with the translate commented out and the rotate not.

Now this works, I run it, the cube increases and spins. However, this is where I’m lost. How does the empty object and the cube suddenly become grouped without me ever doing so? I thought maybe it was the way he named the empty object (Cube “Parent”) which makes the game automatically link the two. So trying this, I create a sphere and a “Sphere Parent”. I attach the two scripts the same way and the sphere moves. So I figure I had it right. Deciding to try one more thing, I delete the pair, re-create them to avoid errors, then attach the move script to the parent. If they’re grouped, then the sphere should move without the script attached to itself. The sphere does nothing.

So here I am, confused like a bat out of hell, not understanding why what he did, did what it did.

Any and all help would be appreciated!

Hi Deathscreton, hierarchy in Unity is based on parenting. You make an empty gameobject called lets say ‘P’ and add the script to it with rotation commented out, click on P in Hierarchy so that you can see it in scene window and run the game Without ‘maximise on play’. You can see that it moves up as it should.

Now the thing is that lets say you make a cube and drag it to P,which is the empty that you had created with the translate script. Since cube is now a child of P, wherever P goes, cube goes with it, however P rotates, cube rotates with it. When you add the script for rotation to cube, all that cube ( the child ) has to do is rotate along its local axes. The job of making it move is done by the parent P.

Try this out : Apart from the rotation script, also add the translate script to cube. Now select P so that you can see it in the Game Window and run the game. You will see that the parent P moves as before but the child cube moves with double the speed in the same direction as the parent. This is again because apart from its own motion, the parent is also moving it. Its like a train is moving towards North with a speed ‘x’ and you are inside the train and running towards North with a local speed ‘x’,so a person seeing you from outside standing 50 meters away sees you running towards north with speed 2x.

As far as the naming convention that you thought of that the word ‘parent’ in the parent name is used so that it is detected by Unity is incorrect. You can name the parent any thing and the child anything else, just make sure that you Drag the object that you wish to be the child on to the object you want to be the parent, on doing this an arrow will show up on the left side pf the parent, which when clicked will show you the children.

Hope this helps!