Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by roody · Jun 06, 2012 at 01:35 PM · c#error

Can anyone help me with a simple CSharp error

Hi all I keep getting error code CS0116 & CS0122 in CSharp

Heres the code im working on:

 using UnityEngine;
 using System.Collections;
 
 public class BaseEntity : MonoBehaviour 

{

     protected float mHealth = 100.0f;
     public float currentHealth{get{return mHealth;}}
     
     protected float mMaxhealth = 100.0f;
     public float maxHealth{get{Type{return mMaxhealth;}}}
 
     public virtual void takeDamage (float amount);

}

 {    ***ERROR CODE CS1016:namespace does not directly contain members***

     if (mHealth <=0)
     mHealth =amount;

 }    ***ERROR CODE CS1022:Type or namespace definition, or end-of-file expected***
 
 {
         die();
 
     
 
       public virtual void healDamage (float amount)
       mHealth = Mathf.Min (mHealth + amount, mMaxHealth);
       protected virtuel void die ()
       Destroy(gameObject);

}

Comment
Add comment · Show 4
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
avatar image syclamoth · Jun 06, 2012 at 01:38 PM 0
Share

Format that a bit better, and possibly the errors will become more obvious. At the very least, you have several spelling errors there- why do you have so many virtual functions? Are you expecting to create subclasses of this?

Your curly braces are all over the place. $$anonymous$$ake sure you know what each and every one of them is for, and where its partner is.

avatar image syclamoth · Jun 06, 2012 at 02:09 PM 0
Share

Thanks for reformatting it. Now, as you can see, it's a complete mess- you have a bunch of code that is completely outside any class. While this would be ok in C, this looks more like C#, and as the error message tells you, you are not allowed to put members (ie, methods and variables) directly in a namespace (not in a class).

Could you explain what the code is supposed to do? It's not clear, looking at your post. Please walk me through it.

avatar image CHPedersen · Jun 06, 2012 at 02:24 PM 0
Share

I pasted all the code into Visual Studio and cleaned it up, indented it properly, and then put the free-floating code into Update as a suggestion. $$anonymous$$aybe that's what you wanted? Also, sycla: The class is called "BaseEntity". That suggests he really does intend to inherit from it, so virtual methods are fine. :)

avatar image roody · Jun 06, 2012 at 02:25 PM 0
Share

Thanks for the reply those 2 are the only errors popping up luckly, im adding weapons in to a FPS type game and the script was ment to be the base for the player and enemies and yea im working in c#.

1 Reply

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

Answer by CHPedersen · Jun 06, 2012 at 02:22 PM

There are multiple things completely wrong with that code. O_O syclamoth pointed out a few of them. I pasted your code into Visual Studio and cleaned it up. Here's what I came up with:

 using UnityEngine; 
 using System.Collections;
 
 public class BaseEntity : MonoBehaviour {
 
     protected float mHealth = 100.0f;
     public float currentHealth{get{return mHealth;}}
 
     protected float mMaxhealth = 100.0f;
     public float maxHealth{get{return mMaxhealth;}}
 
     private void Update() // Added by Christian H. Pedersen. Your if-sentence, that checks the health, did not exist inside any method. I assumed you wanted to check once per frame?
     {
         if (mHealth <= 0)
         {
             //mHealth = amount; Commented out - The variable "amount" does not exist in this scope. What did you want to set mHealth to?
             die(); // Kept in. It makes sence that the entity dies if its health is reduced to 0 or below.
         }
     }
 
     public virtual void healDamage (float amount)
     {   
         mHealth = Mathf.Min (mHealth + amount, mMaxhealth);
     }
   
     protected virtual void die ()
     { 
         Destroy(gameObject);
     }
 }

Judging from the code you posted originally, it looks like you're in dire need of a proper editor. Whatever you're coding in now doesn't indent code properly, which makes it harder for you to keep track of your curly braces. Before you do anything more, get a proper editor, like Visual Studio or at least MonoDevelop.

Secondly, I corrected several errors that were either typos or syntactic errors. For example, the "maxHealth" property you've defined had the word "Type" in front of the block that returns the variable. This makes no sense. You also misspelled "virtual" in the declaration of die, and the identifier "mMaxhealth" was referenced as "mMaxHealth", which did not exist (note the upper case "H").

Anyway, there's the suggestion. Maybe that's what you intended to write. :) Once again: Get a proper editor, it'll help a lot.

Comment
Add comment · Show 1 · 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
avatar image Yuutje · May 02, 2016 at 07:11 PM 0
Share

It don't work for me.

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

help my code has error CS8205 :parsing error how can I fix? 2 Answers

Insert semicolon at the end when theres already one? 0 Answers

targetting dont work 0 Answers

Error in distance coding 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