I have an enemy health script and i counter script. I want to make it so when the enemy’s health gets to 0, the counter will add 1 to it.
Here is the health script #pragma strict
var Health = 100;
var ObjectCounter : ObjectCounter;
ObjectCounter = gameObject.GetComponent("ObjectCounter");
function ApplyDammage (TheDammage : int)
{
Health -= TheDammage;
if(Health <= 0)
{
Dead();
}
}
function Dead()
{
ObjectCounter.Paper+= 1;
Destroy (gameObject);
}
Here is the counter script
var Paper : int = 0;
var paperToWin : int = 30; //number to win!
var sfxHit: AudioClip;
function OnTriggerEnter( other : Collider )
{
if (other.gameObject.tag == "Paper")
{
Paper += 1;
Debug.Log("A paper was picked up. Total papers = " + Paper);
Destroy(other.gameObject);
GetComponent.<AudioSource>().PlayOneShot(sfxHit);
}
}
function OnGUI()
{
if (Paper < paperToWin)
{
GUI.Box(Rect((Screen.width/2)-100, 10, 200, 35), "" + Paper + " Paper(s)");
}
else
{
GUI.Box(Rect((Screen.width/2)-100, 10, 200, 35), "All Papers Collected!");
Application.LoadLevel("Game ending");
}
}
you are getting a reference to the counter script that is attached to the (I’m assuming) enemy GameObject. The problem is that when the enemy’s health gets to zero you do:
Destroy (gameObject);
The fact that you are using gameObject (lowercase ‘g’) means that you are referring to the GameObject that the script is attached to. In other words, when the enemy dies you increase the counter but then destroy the GameObject that has the script that contains the counter. What you’ll need is a GameObject that is separate from your enemies that can keep track of the count and not be destroyed when an enemy dies.
Make a new GameObject, name it something like PaperCounter and add the counter script to it.
Then in your enemy script you can do something like this:
#pragma strict
var health = 100;
var objectCounter : ObjectCounter;
//Here's the important part:
function Start()
{
objectCounter = GameObject.Find("PaperCounter").GetComponent("ObjectCounter");
}
function ApplyDamage (theDamage : int)
{
health -= theDamage;
if (health <= 0)
{
Dead();
}
}
function Dead()
{
objectCounter.Paper += 1;
Destroy (gameObject);
}
Keep in mind that using GameObject.Find() is only one way of achieving this, but will work well if you are instantiating enemies at runtime.
Another problem I see is in the counter script. I assume that the counter should increase when the player collides with a paper. In that case you should move the following code to a script attached to the player (along with relevant components):
#pragma strict
var sfxHit: AudioClip;
var objectCounter : ObjectCounter;
function Start()
{
objectCounter = GameObject.Find("PaperCounter").GetComponent("ObjectCounter");
}
function OnTriggerEnter( other : Collider )
{
if (other.gameObject.tag == "Paper")
{
objectCounter.Paper += 1;
Debug.Log("A paper was picked up. Total papers = " + Paper);
Destroy(other.gameObject);
GetComponent.<AudioSource>().PlayOneShot(sfxHit);
}
}
Some other general notes:
Variables should be named in likeThis while functions should be named LikeThis. It’s a style thing, but makes it clearer what you’re referring to when accessing things in other scripts. I changed some of the names in the above code, but not all of them (eg. Paper). I’ll leave changing the rest up to you.
i think you would need the counter on a seperate script.
Like this:
Enemy health.js(placed on your instantiated enemy)
EnemyManager.js(placed on an empty game object)
So on the enemy manager you have your counter and then each enemy references to that script and increments that variable within the enemy manager script
I think ur problem is that each enemy has a “personal” counter and it’s incrementing that “personal” variable rather than incrementing a public variable that is related to all of them
If you want me to explain further just ask.
I’m not at home atm so I’ll try help further once I get home
@ZefanS Both the Player and and the enemies spawn at the same time, and yes the Player is named “Player” @Orthrinicus Yeah i already do have a separate script for the counter, but when i put the player in the variable, i get the error that i reported earlier. Any ideas?