- Home /
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);
}
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.
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.
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. :)
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#.
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.
Your answer
Follow this Question
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