Im trying to make and attack script that will change the value of my enemy’s health. I’ve look at some tutorials, but I don’t understand them very well. Can someone show me a simple script that can change a value in another script. I tried using the GetComponent function (that’s what they did in the tutorial I watch) in a way similar to this:
target.GetComponent("EnemyHealth")
But it kept giving me error messages that didnt make sense. Am I missing something?
What are the error messages?
It told me to put semi-colons in places that didnt make sense. When I put them there another error message would show up telling me to add something else in an odd place, then it would tell me to take it back off. It generally does this when the syntax is wrong with an if statement. I usually just correct the syntax, but this is the first time i tried to transfer values so I don’t know what’s wrong. The original tutorial I watched did this:
function Attack ()
{
EnemyHealth eh = (EnemyHealth)target.GetComponent(“EnemyHealth”);
eh.AddjustCurrentValue(-10);
}
When I did that I got the error messages. When I tried to tweak it I still got the error messages(though that’s probably because I don’t understand this particular script).
Wierd.
If you stick up the project I might be able to help.
Sorry, but I just started the forum today, so I don’t know how to stick the project up. Unless you just mean post all the scripts.
In unity:
Save the project, and save the scene.
Then go assets → export package.
Then upload the asset file to the provider of your choice (I like dropbox).
I changed my script a bit. Here’s the exact script I’m using now.
var target : GameObject;
function Update ()
{
if(Input.GetKeyUp(KeyCode.Z))
{
Attack();
}
}
function Attack ()
{
var targethealth = target.GetComponent(“EnemyHealth”);
targethealth -= 10;
}
Now when I press “Z” to attack it give me the error message: “NullReferenceException: Object reference not set to an instance of an object.”
In the inspector, did you drag a gameobject onto the scripts ‘target’ property?
Yes, then i tried changing GameObject to Transform, but I got the same message.
You declare targethealth to be the component EnemyHealth from game object (or transform) target. So EnemyHealth now represents a script called EnemyHealth. That’s what the script is doing. Is that right so far?
Next you want to change a variable INSIDE that script minus 10, which is another step. I’m thinking the variable name is targethealth. this is all alittle confusing cause now the script reference and the variable name are the same, so it’s like this:
function Attack ()
{
var targethealth = target.GetComponent("EnemyHealth");
targethealth.targethealth -= 10;
}
You’d want to change the script reference to a better name like this:
function Attack ()
{
var enemyHealth = target.GetComponent("EnemyHealth");
enemyHealth .targethealth -= 10;
}
Now make sure targethealth is a ‘public’ variable and try that.
Thanks, I got the script working now that I understand what it was saying.