- Home /
Question by
Ryanmstc · Jan 16, 2015 at 07:55 AM ·
javascriptgameobjecttransformfirst-person-controllertransform.parent
parent.transform creates new game object
I am trying to make a function where if I press enter, First Person Controller becomes the child of my Vehicle GameObject. My code:
#pragma strict
public var Vehicle;
function Start () {
}
function Update () {
if (Input.GetKeyDown(KeyCode.Return))
{
transform.parent = GameObject(Vehicle).transform;
}
}
The result is when I press enter, it creates a new GameObject called 'New Game Object' and it parents First Person Controller.
Expected Result: When I press enter, the Vehicle GameObject will parent First Person Controller.
How can I reach my expected result?
Comment
Answer by zharik86 · Jan 16, 2015 at 08:40 AM
Command GameObject() create new game object in Scene. Just little correct your scipt:
#pragma strict
public var Vehicle: GameObject; //reference for your object
function Start () {
}
function Update () {
if (Input.GetKeyDown(KeyCode.Return)) {
transform.parent = Vehicle.transform; //Simple get transform
//But if you want youself line, than: tranform.parent = ((GameObject)Vehicle).transform;
}
}
I hope that it will help you.
Thank you. The function works perfectly as expected now.