Hi, I’m trying to make a prototype for a class and the goal is to make a ninja transform into a dragon after picking up gems. When I place the following code into the Update function the dragon appears but keeps being instantiated to infinity:
if (dragonGems == 5){
GameObject dt = GameObject.Instantiate(dragonPrefab, transform.position, transform.rotation)as GameObject;
dt.transform.parent = gameObject.transform.parent;
}
However when I make my own function I pick up 5 gems and nothing happens. It is likely a simple problem to resolve, thanks for the help, here’s the complete code:
using UnityEngine;
using System.Collections;
public class PlayerHP : MonoBehaviour {
public int playerHP;
private int dragonGems = 0;
public GameObject dragonPrefab;
// Use this for initialization
void Start () {
playerHP = 6;
}
// Update is called once per frame
void Update () {
if(playerHP == 0)
Destroy (gameObject.transform.gameObject);
}
void OnTriggerEnter2D(Collider2D other) {
if(other.gameObject.tag == "Weapon")
{
playerHP--;
print("Player has " + playerHP + " HP");
}
if(other.gameObject.tag == "Gem"){
dragonGems++;
print("Player has " + dragonGems + " Dragon Gems");
}
}
void DragonTransformation(int dragonGems){
if (dragonGems == 5){
GameObject dt = GameObject.Instantiate(dragonPrefab, transform.position, transform.rotation)as GameObject;
dt.transform.parent = gameObject.transform.parent;
}
}
}