Parenting woes with Unity3D

This forum is for discussing shmup development, tools, engines and techniques.
Posts: 6
Joined: Wed May 01, 2013 11:50 pm

Parenting woes with Unity3D

Postby Aerosol » Mon May 20, 2013 7:58 pm

Let me start off with a code block, since I find my issue kinda hard to explain.

Code: Select all
#pragma strict

var myTransform : Transform; // for caching
var playfield : Playfield;
var gameManager : GameManager; // used to call all global game related functions
var playerSpeed : float;
var horizontalMovementLimit : float; // stops the player leaving the view
var verticalMovementLimit : float;
var fireRate : float = 0.1; // time between shots
private var nextFire : float = 0; // used to time the next shot
var playerBulletSpeed : float;



function Start ()
{
   myTransform = transform; // caching the transform is faster than accessing 'transform' directly
   myTransform.parent = playfield.transform;
   myTransform.localRotation = Quaternion.identity;
   
}

function Update ()
{
   // read movement inputs
   var horizontalMove = (playerSpeed * Input.GetAxis("Horizontal")) * Time.deltaTime;
   var verticalMove = (playerSpeed * Input.GetAxis("Vertical")) * Time.deltaTime;
   var moveVector = new Vector3(horizontalMove, 0, verticalMove);
   moveVector = Vector3.ClampMagnitude(moveVector, playerSpeed * Time.deltaTime); // prevents the player moving above its max speed on diagonals
   
   // move the player
   myTransform.localPosition += moveVector;


Yes, I'm starting off with monoRail's Unity3D framework!

The problem I'm having is that, unless the Player object is parented to Playfield object in the editor, it doesn't appear to be parented. The Playfield object has a simple script attached that just moves it along the x axis, just to see if the Player is being parented to it. It should be following the Playfield, and it's not.

Now, if I could just parent everything to the Playfield object in the editor, I'd be alright. But I can't do that with objects instantiated at runtime! So I must figure out why my script-method of parenting isn't working.

Thanks for any help.

Site Admin
User avatar
Posts: 240
Joined: Thu Jul 26, 2012 2:32 pm
Location: Melbourne

Re: Parenting woes with Unity3D

Postby monoRAIL » Tue May 21, 2013 3:42 am

I don't think you can access the playfield's transform because you've declared it as type playfield...

var playfield : Playfield;

If you switch it to...

var playfield : GameObject;

then myTransform.parent = playfield.transform; will work. Then if you need to access the playfield's script just use...

playfield.GetComponent(Playfield).functionName();

Posts: 6
Joined: Wed May 01, 2013 11:50 pm

Re: Parenting woes with Unity3D

Postby Aerosol » Fri May 24, 2013 2:42 am

I did as you suggested, and Unity3D told me:
Code: Select all
An instance of type 'UnityEngine.Component' is required to access non static member 'transform'

So I put this right after it:
Code: Select all
playfield = new GameObject("Playfield");


That solved my compiler error, but my player still isn't following the playfield game object. I did this to check it out:
Code: Select all
myTransform = transform; // caching the transform is faster than accessing 'transform' directly
   myTransform.parent = playfield.transform;
   print(myTransform.parent);

And Unity responds with:
Code: Select all
Playfield (UnityEngine.transform)


I've spent the last few days fiddling with this but I can't figure it out. My object is parented correctly, but it won't follow? Baffled.

Site Admin
User avatar
Posts: 240
Joined: Thu Jul 26, 2012 2:32 pm
Location: Melbourne

Re: Parenting woes with Unity3D

Postby monoRAIL » Fri May 24, 2013 12:15 pm

You really don't need your player to follow a playfield anyway, you could just have the playfield scroll backwards, while the camera remains stationary.

Posts: 6
Joined: Wed May 01, 2013 11:50 pm

Re: Parenting woes with Unity3D

Postby Aerosol » Fri May 24, 2013 3:08 pm

I do need the player to follow the playfield. In fact, I need everything to follow the playfield as it moves about in 3D space. I'm making something the along the lines of R-Type Final. My plan is to have a plane (which I'm calling playfield) act as a 2D 'screen' that every object moves in relation to. And that screen will follow a path through a 3D area.

edit:I managed to get it working. I needed to assign something to the playfield variable. I had `playfield = new GameObject("Playfield")` but that's wrong, obviously. I needed to use GameObject.Find to assign the Playfield object that already existed to my playfield variable, which I didn't know about before.

Return to Development

Who is online

Users browsing this forum: No registered users and 4 guests