- Home /
Errors while accessing components
I am currently working on a AI script and its no where near complete but i got this error and i dont understand what its trying to get me to do
using UnityEngine;
using System.Collections;
public class NPC_AI : MonoBehaviour {
public float Speed = 6.0f;
public float rotateSpeed = 6.0f;
public int Range = 20;
public int minHealth = 1;
public int curHealth = 100;
public int maxHealth = 100;
public int GiveXP;
public Transform Player;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(curHealth < minHealth) {
System.Object PlayerController = GetComponent<PlayerController>();
PlayerController.XP += GiveXP();
}
}
}
I have a public int XP in my PlayerController script also and i just dont understand how im getting this error
The error is:
Assets/My Assets/Scripts/NPC/NPC_AI.cs(28,48): error CS1955: The member `NPC_AI.GiveXP' cannot be used as method or delegate
Any help is apppreciated
Answer by GTaveira · Jun 21, 2011 at 12:19 PM
I don't see a method GiveXP() inside your script. You only have a variable of type int with the same name. Try removing the parenthess from the line in which the error occurs:
PlayerController.XP += GiveXP;
But make sure you initialize a value to this variable first, say 10, for instance.
Or, you could actually implement a method called GiveXP(), like this:
public int GiveXP() { return 10; }
In which case you wouldn't need the GiveXP variable.
now im getting this error Assets/$$anonymous$$y Assets/Scripts/NPC/NPC_AI.cs(28,42): error CS1061: Type object' does not contain a definition for
XP' and no extension method XP' of type
object' could be found (are you missing a using directive or an assembly reference?)
What did you change in your code? Could you post it for us?
Answer by Waz · Jun 21, 2011 at 11:47 AM
GiveXP is a variable, not a function so you need:
PlayerController.XP += GiveXP;
i.e. no parentheses.