Search Unity

Please help with error message

Discussion in 'Scripting' started by ExileGamesStudio, Mar 26, 2015.

  1. ExileGamesStudio

    ExileGamesStudio

    Joined:
    Mar 26, 2015
    Posts:
    19
    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.
     
  2. ExileGamesStudio

    ExileGamesStudio

    Joined:
    Mar 26, 2015
    Posts:
    19
    Ive also just noticed that the error is there before I even add anything to it. upload_2015-3-26_19-13-39.png
     
  3. Mathieu_Dossmann

    Mathieu_Dossmann

    Joined:
    Oct 31, 2014
    Posts:
    2
    I think you simply made a typo :
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class BaseWarriorClass : BaseCharacterClass {
    6.     public BaseWarriorClass () {
    7.          CharacterClassName = "Warrior";
    8.          CharacterClassDescription - " A strong and powerful hero";// <- Here you are using "-" instead of "="
    9.          //Replace by CharacterClassDescription =" A strong and powerful hero";
    10.         Stamina = 15;
    11.         Endurance = 15;
    12.         Strength = 20;
    13.         Intellect = 10;
    14.     }
    15. }
     
  4. ExileGamesStudio

    ExileGamesStudio

    Joined:
    Mar 26, 2015
    Posts:
    19
    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
     
  5. ExileGamesStudio

    ExileGamesStudio

    Joined:
    Mar 26, 2015
    Posts:
    19
    but thanks for pointing that out lol Ill have to fix that also. I havnt gone over that one since there was no error attached to it
     
  6. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    Are you still having the issue? What if you try to rename the script to MainCharacterClass
     
  7. ExileGamesStudio

    ExileGamesStudio

    Joined:
    Mar 26, 2015
    Posts:
    19
    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
     
  8. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    You might need to put : MonoBehaviour after your script name

    public class BaseCharacterClass : MonoBehaviour {

    (rest of the script)
     
  9. ExileGamesStudio

    ExileGamesStudio

    Joined:
    Mar 26, 2015
    Posts:
    19
    error.png 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
     
  10. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    Hmm, I'm not sure how to help with that error. Have you tried looking it up? Or maybe someone else can jump in.
     
  11. Mr-Mud

    Mr-Mud

    Joined:
    Mar 8, 2015
    Posts:
    37
    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.
    Code (CSharp):
    1. public enum ItemTypes
    2. {
    3.         EQUIPMENT
    4.         //Etc.
    5. }
    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.
    Code (CSharp):
    1. private ItemTypes ItemType
    2. {
    3.     get{return itemType;}
    4.     set{itemType = value;}
    5. }
    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.
    Code (CSharp):
    1. public int ItemType
    2. {
    3.     get{return (int)itemType;}
    4.     set{itemType = (ItemTypes)value;}
    5. }
    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.
     
  12. ExileGamesStudio

    ExileGamesStudio

    Joined:
    Mar 26, 2015
    Posts:
    19
    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!