I am very new to using unity and to any sort of scripting in general so I have been watching a lot of tutorials. how ever when I follow along with my own script I always have the same error “no monobehaviour scripts in the file or their names do not match the file name” I have gone over the script from the last tutorial which is on how to create an rpg game again and again and mine matches up exactly but I still have the error.
using UnityEngine;
using System.Collections;
public class BaseCharacterClass {
private string characterClassName;
private string characterClassDescription;
//Stats
private int stamina;
private int endurance;
private int strength;
private int intellect;
public string CharacterClassName{
get{return characterClassName;}
set{characterClassName = value;}
}
public string CharacterClassDescription{
get{return characterClassDescription;}
set{characterClassDescription = value;}
}
public int Stamina{
get{return stamina;}
set{stamina = value;}
}
public int Endurance{
get{return endurance;}
set{endurance = value;}
}
public int Strength{
get{return strength;}
set{strength = value;}
}
public int Intellect{
get{return intellect;}
set{intellect = value;}
}
}
the link to the video is
incase you need something to compare it to. its a base character script. there are also 2 other scripts in the video which are different character classes. I have no error on those scripts which is the first time ive never not received the error while trying to follow someones tutorial.
using UnityEngine;
using System.Collections;
public class BaseWarriorClass : BaseCharacterClass {
public BaseWarriorClass () {
CharacterClassName = “Warrior”;
CharacterClassDescription - " A strong and powerful hero";
Stamina = 15;
Endurance = 15;
Strength = 20;
Intellect = 10;
}
}
I really don’t know where to even start with how to fix it so any informations you guys could give me would be greatly appreciated.
using UnityEngine;
using System.Collections;
public class BaseWarriorClass : BaseCharacterClass {
public BaseWarriorClass () {
CharacterClassName = "Warrior";
CharacterClassDescription - " A strong and powerful hero";// <- Here you are using "-" instead of "="
//Replace by CharacterClassDescription =" A strong and powerful hero";
Stamina = 15;
Endurance = 15;
Strength = 20;
Intellect = 10;
}
}
that’s the script that’s not giving me the error. the only once ive done that hasn’t given me the error. Its the fact that the error is there before I even write the script that I really don’t understand
yes im still having the issue. I really don’t know whats causing it now. What would renaming the script do? could the script not be named anything as long as its the same on the script as it is in unity? could you expand on that for me
the script isn’t attached to an object so it doesn’t need monobehaviour. I was able to figure it out though but thanks for your help. I do have one more question though, I found out how to work the console and I found the error that was preventing me from moving further, however I don’t know what it means if someone could help me make a change id appreciate it
The enum ItemTypes is not known outside of the class BaseItem (due to private access modifier), yet other classes need to know it (public access modifier used in your property). There are multiple solutions to solve this issue, so here are a few.
Change the access modifier of ItemTypes to public, as that would make it accessible for all other classes. This is the one I would recommend.
public enum ItemTypes
{
EQUIPMENT
//Etc.
}
Make the property “itemType” private as well. Now there shouldn’t be an access violation… But making it private beats the purpose of this property: making it accessible for other classes.
Lastly you could constantly cast the value from/to the enum type and a widely known type (Integers in the example). This approach, however, goes against the reason why you would want to use an enum in the first place.
public int ItemType
{
get{return (int)itemType;}
set{itemType = (ItemTypes)value;}
}
On another note, code is easier to modify if you post it as text (rather than an image). In case you post code, please use the Code tags; this makes it easier to read. You could then paste your error log elsewhere in the post.
Thanks for the tips guys. I took your advice mr.mud and ive learned a lot more since when I posted this. it actually turned out that the error was from a script I had started working on and then deleted but was still being picked up by unity. Once I removed it from my documents folder the error went away. I appreciate the advice though I will keep that in mind for future questions!