- Home /
Instantiate, problem on transform position
Hello community.
I'm trying to instantiate an object when another is destroyed. But it doesn't Works the transform.position and its instantiated in another side of the scene when it doesn't destroyed.
Here's my code:
using UnityEngine;
using System.Collections;
public class InvocarPowerUp : MonoBehaviour {
public GameObject powerUp;
private float enemigoX;
private float enemigoY;
void start ()
{
enemigoX = transform.position.x;
enemigoY = transform.position.y;
}
public void OnDestroy()
{
Vector3 posicionSpawn = new Vector3(enemigoX, enemigoY);
Quaternion spawnRotation = Quaternion.identity;
Instantiate(powerUp, posicionSpawn, spawnRotation);
}
}
Edit: It sames that it takes the transform.position of the prefab but I need to take the transform.position of the GameObject that it's destroyed and spawn power up there.
Any help will be appreciated. Thanks.
Regards!
Answer by Dragon-Of-War · Dec 30, 2014 at 04:14 AM
First off: 1) void Start is written with uppercase S so its written:
void Start(){
}
//and not
void start(){
}
2) At:
Vector3 posicionSpawn = new Vector3(enemigoX, enemigoY);
Vector3 has to be filled with 3 values: x, y ,z. If you fix and add enemigoZ, it will still spawn at the enemy start point, so i recomend that if you want to spawn the powerup at the enemys current position, then you change:
Instantiate(powerUp, posicionSpawn, spawnRotation);
//to
Instantiate(powerUp, transform.position, spawnRotation);
||||||||||||||||||||||Spanish||||||||||||||||||||||||||||||||
En primer lugar:
1) void Start() se escribe con S mayúscula por lo que su escrito:
void Start(){
}
//e no
void start(){
}
2)En:
Vector3 posicionSpawn = new Vector3(enemigoX, enemigoY);
tiene que ser llenado con 3 valores: x, y, z. Si usted fija y agregar Enemigos, todavía se generan en el punto inicial enemigo, por lo que recomiendo que si quieres generar el arranque en la posición actual del enemigo, entonces cambia:
Instantiate(powerUp, posicionSpawn, spawnRotation);
//a
Instantiate(powerUp, transform.position, spawnRotation);
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
When i press the key to run the animation it pauses my game! HELP! 1 Answer
lighting script help 1 Answer
Not able to fire projectiles at mouse position 2 Answers
prefab always instantiates at 0.0.0 1 Answer