Hi! I am trying to add the playerobject to my train object as a child, so it will move with the train when it moves instead of standing still. I created a manageChildren script, which simply makes a given object the child of the object which the script is on. This works perfectly but not when I want to call it from my gameController script on my preload scene.
The function is being called since I can get a debug message from it, but it just won’t change the parent of the object.
This is my manageChildren script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class manageChildren : MonoBehaviour
{
public GameObject childToBe;
public void ConnectChild()
{
childToBe.transform.parent = gameObject.transform;
}
public void DisconnectChild()
{
childToBe.transform.parent = null;
}
}
This is how I call the function (from the preloader scene which has a DDOL script):
public class GameController : MonoBehaviour
{
private manageChildren trainAndPlayer;
void Start()
{
StartCoroutine(LoadScene(sceneIterator));
trainAndPlayer = GameObject.Find("Trains").GetComponent<manageChildren>();
trainAndPlayer.ConnectChild();
}
private IEnumerator LoadScene(int sceneID){}
}
I hope somebody can help me out. Thanks in advance!