- Home /
How to call functions?
Hello, I've made a script that tries to access another script to do damage to an enemy and I've hit a bump in the road. I have a script thats attached to my camera which contains a Update function that lets you select an enemy and also it has an else statement. That else statement's supposed to run a function called HitTarget if you press 1. I keep having a error when I run the game. I don't get what i'm doing wrong but its probably a simple mistake. Heres the two scripts that i have.
//Targeting.js attached to camera
var target : GameObject; //The target you want to attack
var EnemyTargetted : boolean = false;
public var damage = 7;
public var health = 74;
function Update (){
//find the enemy you want to attack
var hit : RaycastHit;
if(Input.GetButtonDown("Fire1")){
var ray : Ray = gameObject.camera.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit)){
if(hit.collider.tag == "Enemy"){
target = hit.collider.gameObject;
EnemyTargetted = true;
}
else{
//normal attack by pressing 1
if(Input.GetKeyDown ("1")) {
HitTarget ();
}
}
}
}
}
function HitTarget (damage : int) {
target.GetComponent(NewBehaviourScript).Hit(damage);
}
Heres the script that the enemy has (still in progress).
var damage = 3;
var InSight : boolean;
var startingHealth = 75;
private var enemyHealth : int;
function Start () {
enemyHealth = startingHealth;
}
function Hit (damage : int) {
enemyHealth -= damage;
if (enemyHealth <= 0) {
Destroy (gameObject);
}
}
function OnTriggerEnter (other : Collider){
if(other.tag == "Player"){
InSight = true;
}
}
function OnTriggerExit (other: Collider){
if(other.tag == "Player"){
InSight = false;
}
}
I get this error when i try to run the game :The best overload for the method 'Targeting.HitTarget(int)' is not compatible with the argument list '()'.
Please help I can't continue my game without this being fixed.
Answer by Eric5h5 · Feb 01, 2013 at 12:00 AM
As the error says, you're doing "`HitTarget()`", but the function you have requires that you supply an int.
function HitTarget (damage : int) {
That declares HitTarget to require an integer for damage. You're calling HitTarget() with an empty set of arguments. Try:
var dmgamount : int = 10; HitTarget(dmgamount);