Instantiate blocks the reading of remaining lines in method
Hello everyone ! Good day to you.
I'm currently struggling with a GameObject Instentiation from a prefab ; the precise line of code I use for this seems to mess up the behaviour of the void I place it in no matter what.
I use this line :
myPannel = Instantiate (pannelPrefab, new Vector3 ((gameObject.transform.position.x+cr.transform.position.x)/2, gameObject.transform.position.y-5, (gameObject.transform.position.z+cr.transform.position.z)/2), Quaternion.identity);
"myPannel" is an empty public GameObject, pannelPrefab is a GameObject : I dragged and dropped my prefab in the Inspector.
When make the line appear in Awake, it disable the script. When I make it appear on Start or on a Method I use once in update, the method will stop reading and I'll go back to Update. I tried void and Coroutines, it doesn't seems to change much.
Here are the parts of code concerned :
public MeshRenderer myMeshRenderer;
public GameObject pannelPrefab;
public GameObject myPannel;
bool init = false;
void Awake () {
//cr = GameObject.Find ("Controller (right)");
//VM = this.gameObject.GetComponent<ValueManager> ();
myMeshRenderer = gameObject.GetComponent<MeshRenderer>();
//myPannel = Instantiate (pannelPrefab, new Vector3 ((gameObject.transform.position.x+cr.transform.position.x)/2, gameObject.transform.position.y-5, (gameObject.transform.position.z+cr.transform.position.z)/2), Quaternion.identity);
}
In here, cr and VM were supposed to be called but are never. I managed to work around, and myMeshRenderer is called correctly at this moment. If I don't comment myPannel, the script desactivates itself.
void Start(){
//myPannel = Instantiate (pannelPrefab, new Vector3 ((gameObject.transform.position.x+cr.transform.position.x)/2, gameObject.transform.position.y-5, (gameObject.transform.position.z+cr.transform.position.z)/2), Quaternion.identity);
//cr = GameObject.Find ("Controller (right)");
VM = gameObject.GetComponent<ValueManager> ();
//IM = cr.GetComponent<InputManager> ();
GameObject gpg = GameObject.Find("ValueG");
GameObject gpf = GameObject.Find("ValueF");
//myPannel = Instantiate (pannelPrefab, new Vector3 ((gameObject.transform.position.x+cr.transform.position.x)/2, gameObject.transform.position.y-5, (gameObject.transform.position.z+cr.transform.position.z)/2), Quaternion.identity);
pg.GetComponentInParent<Transform>().tag = "G";
pg.gameObject.tag = "G";
pf.GetComponentInParent<Transform>().tag = "F";
pg.gameObject.tag = "F";
}
I tested it out on Start here in various place : Whatever was placed before is read, the line and the following is ignored.
void Update(){
if (!init) {
Debug.Log("First Update");
TheInit ();
}
}
void TheInit(){
Debug.Log("Init");
myPannel = (GameObject)Instantiate (pannelPrefab, new Vector3 ((gameObject.transform.position.x+cr.transform.position.x)/2, gameObject.transform.position.y-5, (gameObject.transform.position.z+cr.transform.position.z)/2), Quaternion.identity);
Debug.Log("Init2");
myMeshRenderer.enabled = false;
myPannel.SetActive (false);
init = true;
}
Here again, Debug.Log("First Update") and "Init" are proprely read, but never "Init2". If i Place init=true before my Instentiation, it reads normally, and TheInit() is never called again.
This is a project I've been working on for days and I managed to make it work before, messing up with the order of call. But trying different set ups in order to displace the Instence or make it spawn in different placed, I lost track and am a bit lost in the understanding of how the whole thing works.
Thanks to all, and have a nice day !