- Home /
dont instantiate clones
hey, im making a game, i want it so when the player hits a box a plane pears somehow and the player is destroyed so the plane turns into the player, but several problems hapened:
when the player colides with the box nothing hapens
when the plane is created and the player is destroyed, the plane wont move or use anny physics though it has a rigid body. so here is the code
var player : String; var ObjectToDeploy : GameObject; var bullit : GameObject; var deploy = false; function Update () { if (deploy) { Instantiate(ObjectToDeploy, Vector3(transform.position.x, transform.position.y + 10, transform.position.z), transform.rotation); deploy = false; Destroy (gameObject); Destroy (GameObject.Find("Player")); } } function OnTriggerEnter (hit : Collider){ if (hit.gameObject.tag == player){ deploy = true; } }
so plz let me know what can i do or some way to optimize it
Answer by hijinxbassist · Jun 19, 2012 at 02:08 AM
For the plane, make sure you add the Move script to the objectToDeploy s gameObject.
Does your player string match the players tag, thats something to check (make sure its not empty also).
No reason to deploy to false since you destroy this GO anyway. You could also optimize that like this(get rid of deploy boolean and just call Deploy()
function OnTriggerEnter (hit : Collider)
{
if (hit.tag == player)
{
Deploy(hit.gameObject);
}
}
function Deploy(myPlayer:GameObject)
{
var dObj=Instantiate(ObjectToDeploy, Vector3(transform.position.x,
transform.position.y + 10, transform.position.z), transform.rotation)as GameObject;
dObj.AddComponent(PlayerMove);
//add other components
dObj.tag==player;//change tag if necessary
Destroy (myPlayer);
Destroy (gameObject); //Call this 2nd, otherwise this script is already gone
}
Did you add the move script to the new plane? Are you moving based on something like a character controller?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Instantiate prefab to the parent 2 Answers
I need help with rotation 2 Answers
Problem with colliding instances of same object 1 Answer
Instantiated object not colliding when not have a rigidbody?? 2 Answers