- Home /
The question is answered, right answer was accepted
instantiate object
Hello there, i have a script for bomb creation and destroy with explosion particles. Here is my script definition, i try to create a bomb by pressing space bomb creation it will automatically destroy after 3 seconds, that time i try to instantiate explosion particle. My bomb is created and destroyed perfectly but the particle wasn't. Here is the snippet.
using UnityEngine;
using System.Collections;
public class PlayerMove : MonoBehaviour
{
//AI Movement Variables...
public float Speed = 2.0f;
public Vector3 MoveDirection_Left;
public Vector3 MoveDirection_Right;
public GameObject Bomb;
private GameObject Bomb_Temp;
public GameObject Explosion;
private GameObject Explosion_Temp;
public static bool Bomb_Click, Bomb_Visible;
public float Bomb_Time = 0;
public float Explosion_Time = 0;
// Use this for initialization
void Start ()
{
MoveDirection_Left = Vector3.forward;
MoveDirection_Right = Vector3.right;
Bomb_Click = false;
Bomb_Visible = false;
}
void Bomb_Creation()
{
Bomb_Temp = Instantiate (Bomb, transform.position, transform.rotation) as GameObject;
}
void Bomb_Destroy()
{
Destroy(Bomb_Temp, 3.0f);
}
void Bomb_Explosion()
{
Explosion_Temp = Instantiate (Explosion, Bomb_Temp.transform.position, Bomb_Temp.transform.rotation)as GameObject;
}
void Explosion_Destroy()
{
Destroy(Explosion_Temp, 3.0f);
}
void Update()
{
// Character Movement...
if(Input.GetKey(KeyCode.RightArrow))
{
transform.Translate(-MoveDirection_Right
* Speed * Time.deltaTime);
}
if(Input.GetKey(KeyCode.LeftArrow))
{
transform.Translate(MoveDirection_Right
* Speed * Time.deltaTime);
}
if(Input.GetKey(KeyCode.UpArrow))
{
transform.Translate(-MoveDirection_Left
* Speed * Time.deltaTime);
}
if(Input.GetKey(KeyCode.DownArrow))
{
transform.Translate(MoveDirection_Left
* Speed * Time.deltaTime);
}
// Create Bomb...
if(Input.GetKeyDown ("space"))
{
Bomb_Click = true;
}
if(Bomb_Click == true)
{
Bomb_Creation();
Explosion_Destroy();
Debug.Log ("Bomb Created");
Bomb_Click = false;
Bomb_Visible = true;
}
if(Bomb_Visible == true)
{
Bomb_Destroy();
Bomb_Explosion();
Bomb_Visible = false;
}
}
}
-Prasanna
Please format your code. If you don't know how, watch the tutorial video on the right.
Answer by Em3rgency · May 23, 2014 at 12:36 PM
Off the top of my head, I'd say switch Bomb_Explosion() and Bomb_Destroy() places.
You're destroying the bomb, but then using its transform (non existent, because the bomb is destroyed) for the explosion. Call the explosion first, THEN remove the bomb.
Answer by Prasanna · Dec 04, 2014 at 05:02 AM
Here it is, i fix this problem.
if(Input.touchCount > 0)
if(Bomb_Button.HitTest(Input.GetTouch(0).position))
{
if (Instance == true)
{
Bomb_Temp = Instantiate(Bomb, Bomb_Place.transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
Explosion_Position = Bomb_Temp.transform.position;
Explosion_Rotation = Bomb_Temp.transform.rotation;
Bomb_Click = true;
Instance = false;
}
}
if(Bomb_Click == true)
{
Bomb_Time += Time.deltaTime;
if(Bomb_Time >= 3f)
{
Destroy(Bomb_Temp);
Bomb_Time = 0;
Bomb_Click = false;
Bomb_Visible = true;
Expose_Instance = true;
}
}
if(Bomb_Visible == true)
{
if(Expose_Instance == true)
{
audio.PlayOneShot (Explosion_Sound);
Explosion_Temp = Instantiate (Explosion, Explosion_Position, Explosion_Rotation)as GameObject;
Expose_Instance = false;
}
Explosion_Time += Time.deltaTime;
if(Explosion_Time >= 2f)
{
Destroy(Explosion_Temp);
Explosion_Time = 0;
Bomb_Visible = false;
Instance = true;
}
}