- Home /
Change value one prefab NOT ALL
I spent half a day finding how to change value one prefab NOT ALL
This is my example:
First, I attach this script "Enemy_Health" to the enemy prefab:
//My enemy's health is 2
static var HEALTH = 2;
Second, I tag my prefab enemy "enemy", then attach this script to my bullet:
function OnTriggerEnter(hit : Collider) {
if(hit.gameObject.tag=="enemy")
{
//get the script "Enemy_Health" and then subtract the health of enemy from the script
var enemy : Enemy_Health = gameObject.GetComponent(Enemy_Health);
enemy.HEALTH -=1;
//Destroy only an enemy prefab collider, not all prefab
if(enemy.HEALTH <=0)
{
Destroy(hit.gameObject);
}
}
}
I don't know my script didn't work as my expectation( change value HEALTH one prefab and destroy that prefab NOT ALL ).
In this case, kill the enemy after shooting 2 times
On contrast, what my script did is when my bullet hit the obJect, other instantiating enemyprefabs are destroyed immediately and other left over enemyprefabs in my screen game are destroyed by only ONE buttlet, not 2.
Someone know this problem?
Thanks for your time, regards.
Answer by Joshua · Jun 25, 2011 at 02:31 PM
You have the variable HEALTH marked as static. Normal variables 'exist in their instance', meaning that if you have two enemies with the script
var health : int;
then you can set the health of one of them to 50 and the health of the other to anything else.
Static variables however 'exist in the scene'. There is only one of them at all times. I don't know why you have HEALTH be a static variable in this case, but it does exactly what it does - if you change it in one instance of the script it changes for all.
I'm guessing the reason you did it here is so you can access it more easily? scriptName.theStaticVariable is how you access a static variable because for that scriptName there is only one version of theStaticVariable. If you want to access a normal variable you have to get it from that instance of the script using GetComponent.
[edit] here's what it should look like. The first script is called Enemy_Health, the second is called whatever you want.
var health : int = 2;
that's the first, then the second:
function OnTriggerEnter( hit : Collider )
{
if( hit.GameObject.tag == "Enemy" )
{
var enemyHealth : int = (hit.gameObject.GetComponent( Enemy_Health ) as Enemy_Health).health;
enemyHealth--;
if( enemyHealth <= 0 )
{
Destroy( hit.gameObject );
}
}
}
I know and try to fix my script but it's no result. Because when I use your hint with this script: var enemy : Enemy_Health = gameObject.GetComponent(Enemy_Health); enemy.HEALTH -=1;
I get the report "HEALTH" is not an static member of "Enemy_Health"
On the other hand, would you know another script that destroy the enemy prefab after 2 hit? I know how to do this with 1 enemy, but with multiple enemy instantiating over time, I don't know :(
Updated the answer with how I would do it. After going through your script again I spotted the error you say
var enemy : Enemy_Health = gameObject.GetComponent(Enemy_Health);
but you want to access this script in the enemy, not on yourself. So make it hit.gameObject.GetComponent.....
Thank Joshua, That's better but not effective, I don't know why enemy can't be destroy, so I add this line after the line enemyHealth--; print(enemyHealth); and ops, the report is always "1" ? what happened here?
You have to go out of the trigger and re-enter for it go decrement again
An integer is a value type. When you assign it to a local variable only the value gets copied. If you change the local variable the original variable won't change.
if( hit.GameObject.tag == "Enemy" )
{
var enemy : Enemy_Health;
enemy = hit.gameObject.GetComponent(Enemy_Health);
enemy.health--;
if( enemy.health <= 0 )
{
Destroy( hit.gameObject );
}
}
Answer by superstar123 · Jun 25, 2011 at 05:33 PM
Thanks a lot Bunny83. great code, tested and it works :) However thank Joshua for you time ;)
Your answer
Follow this Question
Related Questions
Destroyed prefab still moving 1 Answer
some prefab collisions randomly stop working 2 Answers
Object.Destroy destroying all prefabs 1 Answer
Destroy gameObject leaves behind collider? 1 Answer
Play animation before death...c# 1 Answer