- Home /
Problem with GetComponentinChildren and Instantiate
Hello, I'm trying to make a code that allow me with little mesh called modules to procedurally create a dungeon. My modules have empty gameObject has children named exit. My goal is to make 2 exits of 2 different modules match in the world space. To do that, i was thinking that i could recuperate the transform.position of my exit with the command GetComponentInChildren. Also i'm "spawning" my module in the world with a Instantiate command but if i try to get the transform.position of my exit, it gives me the position of the prefab (0,0,0) and not the child but the parent transform.
using UnityEngine;
using System.Collections.Generic;
public class WorldGenerator : MonoBehaviour {
public GameObject Modules;
public GameObject StartModule;
public int itt = 5;
private Vector3 Exit;
private Vector3 ModExit;
// Use this for initialization
void Start () {
////////It's where i'm blocking
GameObject startmodule = (GameObject)Instantiate (StartModule, transform.position, transform.rotation);
Exit = startmodule.GetComponentInChildren<Transform>().position;
//////////////////////////////////////////////////////////////
Instantiate (Modules, Exit, transform.rotation);
ModExit = Modules.GetComponentInChildren<Transform> ().position;
Debug.Log (Exit); //return (0,0,0), the prefab transform in my prefab folder
Debug.Log (ModExit);
}
// Update is called once per frame
void Update () {
}
}
I tried to store my Instantiate into a variable and then ask the transform to this variable but it still give me 0,0,0. Help will be appreciated and thanks for your response.
PS : My script is attached to an empty gameobject in my scene PS2 : sorry for killing Shakespeare language
Your answer
Follow this Question
Related Questions
Can't move instantiated prefab 2 Answers
Saving customized transform in game 1 Answer
(Unity 2D C#) Move instantiated prefab on Y axis? 1 Answer
Get transform of instantiated Prefab; Help appreciated. 1 Answer
Why Transform and not Prefab? 2 Answers