(RPG) MissingReferenceException : the object of type "Transform" has been destroyed but you are still trying to access it.
I have a problem on my Turn based RPG. My Character can attack, on melee and Ranged, but the problem is on the Ranged Attack. somehow it didn't spawn projectiles when i press the ranged attack button. it DID Spawn on the Hierarchy, but it didn't show on the scene, and it doesn't travel horizontally here's the attack sample:
public static int HP = 100;
public static int maxHP = 100;
public Transform Projectile;
public Transform DmgTextObj;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if ((Input.GetKeyDown (KeyCode.Q)) && (BattleFlow.whichTurn == 1)) { // BattleFlow.whichTurn == 1 as a turn indicator
AttackMelee ();
StartCoroutine (returnChar ());
}
if ((Input.GetKeyDown (KeyCode.W)) && (BattleFlow.whichTurn == 1)){
AttackRanged ();
StartCoroutine (returnChar ());
}
}
void AttackMelee(){
BattleFlow.currentDamage = 50;
GetComponent<Transform> ().position = new Vector2 (2f, -0f); // karakter maju
}
IEnumerator returnChar(){
yield return new WaitForSeconds (1); //short delay before going back to original position
GetComponent<Transform> ().position = new Vector2 (-5f, 0f);
BattleFlow.whichTurn = 2;//BattleFlow.whichTurn == 2
Instantiate (DmgTextObj, new Vector2 (5f, 5.5f), DmgTextObj.rotation);
}
void AttackRanged(){
BattleFlow.currentDamage = 30;
GetComponent<Transform> ().position = new Vector2 (-3f, 0f); //char movement
Instantiate (Projectile, new Vector2 (), Projectile.rotation); //& spawning projectile
}
and here's the projectile script sample:
void Start () { GetComponent ().position = new Vector2 (5f, 0f); Destroy (gameObject, 2.5f); }
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D other){
if (other.CompareTag ("Enemy"))
{
//Instantiate (ExplosionEffect, transform.position, transform.rotation);
Destroy (other.gameObject);
Destroy (gameObject);
}
}
Pls help me.
Your answer
Follow this Question
Related Questions
Why more balls are instantiating? 0 Answers
IndexOutOfRangeException error when getting transform of array object 0 Answers
Can't apply AddForce after Instantiating object 1 Answer
How to spawn objects on a list of Transforms ? 2 Answers
Spawn in a different and random Spawn point from a list 0 Answers