Instantiate gameObject in custom position don't work
I everyone!
I've the following code in a script inside a GameObject called "build_spawnwer"
helicopter = (GameObject)Resources.Load("prefab/helicopter", typeof(GameObject));
Vector3 incremet_pos = new Vector3(0, 7.5f);
Vector3 copter_pos = this.transform.position + incremet_pos;
Instantiate(helicopter, copter_pos, Quaternion.identity);
The problem is that the prefab "helicopter" isn't create in the position that I indicates at the second param of the Instantiate method but is instantiate in the "build_spawner" position...
I've no more prufs to do... Please help me! thanksss
Answer by tormentoarmagedoom · Jun 21, 2018 at 04:25 PM
Good day. Thi is beaause Your stored prefab has some position stored. Drag it into the scene, change its position to 0,0,0 and apply changes to the prefab to save the new position, and it will work.
But you have another option, do this for Isntantiate; store it in a variable so you can move it after instantiate like any other gameobject.
GameObject NewHelicopter = Instantiate(helicopter, copter_pos, Quaternion.identity);
NewHelicopter.transform.position = copter_pos;
Bye! :D
Hi, thanks for your response!
I've tryed the two ways and... nothing :(
I change the position of the prefab to 0, 0, 0 and nothing...
And later I change the code to:
helicopter = (GameObject)Resources.Load("prefab/helicopter", typeof(GameObject)); Vector3 incremet_pos = new Vector3(0, 7.5f); Vector3 copter_pos = this.transform.position + incremet_pos; GameObject NewHelicopter = Instantiate(helicopter, copter_pos, Quaternion.identity); NewHelicopter.transform.position = copter_pos;
and nothing :((( Please help
Follow this Question
Related Questions
I get an error message every time i run this script? Any ideas? 0 Answers
Unable to set position of instantiated prefab 1 Answer
My code is instantiating many prefabs, i only want one. 1 Answer
Generating prefabs at origin that are children of moving GameObjects? 2 Answers
How to spawn a prefab at a duplicate objects location 1 Answer