- Home /
train problems
In the game i am currently working on i have a train system that serves to take the player across an island, the train is moving on a loop through the animation system, the problem is the player wont stay on the train i have tried everything from coliders to fixed joints nothing seems to work this is the current code of the train:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class train_board_confusion : MonoBehaviour {
public GameObject train;
public GameObject item;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
item.transform.parent = train.transform;
}
void OnTriggerEnter(Collider other)
{
item = other.gameObject;
}
}
the current error is saying it cant change the parent of a prefab, please help im relatively new to game development and c# so im probably missing something stupid.
I am going to assume you assigned the player prefab to the "item" variable in the inspector, or some such.
You want to change the parent of the player instance, not the player prefab. To do that, do the parenting in OnTriggerEnter, and remove the item variable.
public GameObject train;
void OnTriggerEnter(Collider other)
{
other.transform.parent = train.transform;
}
void OnTriggerExit(Collider other)
{
other.transform.parent = null;
}
However, this may not work, depending on how the player's movement system works.
thanks for your response, however now i have a new error:
gameobject dose not contain a definition for parent
Whoops, forgot the .transform. $$anonymous$$y bad. I edited the original comment.
now there are no errors in visualstudio but we're back to the problem where unity says that "setting the parent of a transform which resides in a prefab is disabled to prevent data corruption"
Can you post your modified whole script again? Something doesn't sound right.
the current script is exactly like you said :
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class train_board_confusion : $$anonymous$$onoBehaviour { public GameObject train;
void OnTriggerEnter(Collider other)
{
other.transform.parent = train.transform;
}
void OnTriggerExit(Collider other)
{
other.transform.parent = null;
}
}
update i just atempted to use the same code on a default cube game object and it sucessfuly made the player a child of it the problem must be with my train object
Answer by niorg2606 · Aug 07, 2017 at 11:22 PM
With the animation, I'm guessing you're just changing it's position. If that's the case, the only way I could think of to have the player move with the train is if you set the player's position as well. As for your error, I'm assuming that item is a prefab. If you want to keep it this way, I'd suggest instantiating the item first, which you can read up on how to do here. Basically, you just make a new local variable and set it to the instantiation like this: GameObject newItem = Instantiate<GameObject>(item);
.
Thanks for your response however i was able to fix the problem with bobisgod234's help, i just added a empty game object with a collider to my train and used the script above in it.