- Home /
Script Deleting itself on run,
I have a Small MMO project (not to be published, might if I pretty it up) and it has an issue. My Quest are basic. Kill a random amount of Trolls, and when you reach that amount, you go back to the over world. but my script that counts the kills is Deleting itself from my character when I run the game in Unity. how do I fix it?
My KillEnemy Script
using UnityEngine;
using System.Collections;
public class KillEnemy : MonoBehaviour {
int EHealth = 100;
int Damage = 25;
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "Fireball") {
EHealth -= Damage;
}
if (EHealth <= 0) {
Destroy (gameObject);
TKO tko;
tko = GetComponent<TKO> ();
tko.KillTroll ();
}
}
}
And this is my KillOutput Script:
using UnityEngine;
using System.Collections;
public class TKO : MonoBehaviour {
public int TrollKilled = 0;
public int MaxNeeded;
void Start() {
MaxNeeded = Random.Range (10, 51);
}
// Update is called once per frame
public void KillTroll () {
TrollKilled ++;
}
void OnGUI() {
GUI.Box (new Rect (0, 0, Screen.width / 2, 20), TrollKilled + "/" + MaxNeeded);
}
void Update () {
if (TrollKilled <= MaxNeeded)
Application.LoadLevel (1);
}
}
EDIT I've Updated The KillEnemy Script,
using UnityEngine;
using System.Collections;
public class KillEnemy : MonoBehaviour {
int EHealth = 100;
int Damage = 25;
private TKO tko;
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "Fireball") {
EHealth -= Damage;
}
if(EHealth <= 0)
Destroy (gameObject);
AddPoint ();
}
void AddPoint() {
tko = GetComponent<TKO> ();
tko.KillTroll ();
}
}
TKO Vanishes from the Hierarchy When I Press play.
The script is there, but it dissapears when I click play, And I'd rather not replace my work with PlayerPrefs ($$anonymous$$ostly because they're $$anonymous$$d boggling.)
By disappear do you mean removed or disabled? Before you post the script, are you using Destroy() at all? Perhaps you are calling Destroy(this) which removes the script component from the game object? If not, it would help to see the script :P
Removed, But I'll go edit my OP and add the two scripts that make the $$anonymous$$ill Output work (Supposedly)
Did you put this code on your own character? I think this code should put on your monsters.
Your answer
Follow this Question
Related Questions
How to use Configurable Joints and DragRigidBody Script with a door 0 Answers
Changing a variable in another script for one object. 2 Answers
How to change texture of another object by clicking on a object? 0 Answers
detecting whether the object has been clicked in another script 2 Answers
How to communicate between scenes??? 2 Answers