Mob.opponent' is a `field' but a `type' was expected help please! :)
i get this error Assets/myscripts/Mob.cs(62,57): error CS0118: Mob.opponent' is a
field' but a `type' was expected here is my code
using UnityEngine;
using System.Collections;
public class Mob : MonoBehaviour
{
public float speed;
public float range;
public CharacterController controller;
public Transform player;
private Fighter opponent;
public AnimationClip run;
public AnimationClip idle;
public AnimationClip die;
public AnimationClip attackClip;
public double impactTime = 0.36;
public int health;
public int damage;
private bool impacted;
// Use this for initialization
void Start ()
{
opponent = player.GetComponent<Fighter> ();
}
// Update is called once per frame
void Update ()
{
Debug.Log (health);
if(!isDead ())
{
if(!inRange ())
{
chase ();
}
else
{
animation.Play(attackClip.name);
}
}
else
{
dieMethod();
}
}
void attack()
{
if(animation[attackClip.name].time>animation[attackClip.name].length*impactTime&&!impacted)
{
opponent.getHit
impacted = true;
}
}
bool inRange()
{
if(Vector3.Distance(transform.position, player.position)<range)
{
return true;
}
else
{
return false;
}
}
public void getHit(int damage)
{
health = health - damage;
if(health<0)
{
health = 0;
}
}
void chase()
{
transform.LookAt(player.position);
controller.SimpleMove(transform.forward*speed);
animation.Play(run.name);
}
void dieMethod()
{
animation.Play(die.name);
if(animation[die.name].time>animation[die.name].length*0.9)
{
Destroy(gameObject);
}
}
bool isDead()
{
if(health <= 0)
{
return true;
}
else
{
return false;
}
}
void OnMouseOver()
{
player.GetComponent<Fighter>().opponent = gameObject;
}
}
Answer by robertbu · Nov 23, 2014 at 02:41 PM
Your problem is on the previous line...line 62 in the pasted code:
opponent.getHit
If 'getHit' is a variable, you don't assign to it or assign it to some value. If it is a function, you don't call it. So the compiler is getting confused thinking this is an attempt to declare the variable type for 'impaced' in the following line. Fix (or comment out) this line, and this part of your script will compile.
Your answer
Follow this Question
Related Questions
(Array) Is a 'field' but is used like a 'type' 1 Answer
http://answers.unity3d.com/questions/8472/help-with-error-expression-denotes-a-type-where-a.html 1 Answer
Why is it giving me this error "cannot implicitly convert type 'int' to 'string'" 3 Answers
One script with multiple Gameobject only one Works 0 Answers
Unity bug gives false error messages or doesen't work without a Debug.Log() line present 1 Answer