• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by vevebe · Nov 23, 2014 at 02:38 PM · errorgameobjecttypefield

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;
                 }
     
     
 
 
             
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

(Array) Is a 'field' but is used like a 'type' 1 Answer

Why is it giving me this error "cannot implicitly convert type 'int' to 'string'" 3 Answers

http://answers.unity3d.com/questions/8472/help-with-error-expression-denotes-a-type-where-a.html 1 Answer

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges