Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

C# Can't get switches to work correctly

Discussion in 'Scripting' started by jolo309, Nov 21, 2013.

  1. jolo309

    jolo309

    Joined:
    Mar 4, 2012
    Posts:
    94
    Hello, i can't seem to get switches to work correctly, this is my code:

    Code (csharp):
    1. public enum classes
    2.     {
    3.         Warrior,
    4.         Mage,
    5.         Rogue,
    6.         Paladin
    7.     }
    8.     public classes Classes = new classes();
    9.  
    10.     void Start()
    11.     {
    12.  
    13.         HealthScript hs = GetComponent<HealthScript>();
    14.  
    15.         //ClassManager();
    16.  
    17.         switch(Classes)
    18.         {
    19.         case classes.Warrior:
    20.             hs.maxHealth = 158.0f;
    21.             break;
    22.         case classes.Mage:
    23.             hs.maxHealth = 100.0f;
    24.             break;
    25.         case classes.Rogue:
    26.             hs.maxHealth = 126.0f;
    27.             break;
    28.         case classes.Paladin:
    29.             hs.maxHealth = 144.0f;
    30.             break;
    31.         }
    32.  
    33.  
    34.     }
    So i get this error

    Code (csharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. ClassesScript.Start() (at Assets/Scripts/ClassScript.cs:25)
    I've looked at the code for a day and a few hours now but still can't figure it out why it keeps saying that, i even tried to do it from the HealthScript.cs but i get the same error so i can't see what the problem is. I also tried with if statements and it's the same error, can't be because of the F at the end of the numbers because i tried to remove those too.

    I'm still learning C# so i am still need for some advice or anything that helps me fix this
     
  2. cjddmut

    cjddmut

    Joined:
    Nov 19, 2012
    Posts:
    177
    There's something I find a little odd. I don't think you need to 'new' an enum.

    http://msdn.microsoft.com/en-us/library/sbbt4032(v=vs.110).aspx

    So do something like this.

    Code (csharp):
    1.  
    2. public enum classes
    3.     {
    4.         Warrior,
    5.         Mage,
    6.         Rogue,
    7.         Paladin
    8.     }
    9.  
    10.     public classes Class = classes.Warrior;  // In this case Warrior is simply the default.
    11.  
    12.  
    I don't know or think that would cause your exception though. I would confirm that hs is coming back as not null. That would certainly cause a null dereference exception.
     
  3. jolo309

    jolo309

    Joined:
    Mar 4, 2012
    Posts:
    94
    Yeah line 20 is 25 because i didn't copy my whole script so it is something with the hs.maxHealth but i just don't know, what do you mean that it is not coming back as not null i mean like i know what it means but how could i fix it i am wondering. Might help if i pasted my healthscript too?
     
  4. cjddmut

    cjddmut

    Joined:
    Nov 19, 2012
    Posts:
    177
    I doubt it has to do with the healthscript itself, I would sanity check to make sure this script only runs on a GameObject that has the HealthScript script on as well.
     
  5. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Here is how you can test.

    Code (csharp):
    1.   HealthScript hs = GetComponent<HealthScript>();
    2. #if UNITY_EDITOR // This will only run in Unity. Not on any published build.
    3.   if (hs == null) {
    4.     Debug.LogError("Could not find a HealthScript component on GameObject " + name);
    5.   }
    6. #endif
    If hs == null that means that your HealthScript isn't already attached to the GameObject or it's on a different GameObject.
     
  6. jolo309

    jolo309

    Joined:
    Mar 4, 2012
    Posts:
    94
    Both Scripts are on same GameObject now, it did work thanks. Anyway to do it without having it on same GameObject? Or is that impossible
     
    Last edited: Nov 21, 2013
  7. roooob

    roooob

    Joined:
    Jun 20, 2013
    Posts:
    24
    You can also use [RequireComponent(ComponentType)] to ensure that other necessary components are attached to a given GameObject.

    Example:

    Code (csharp):
    1.  
    2. [RequireComponent(typeof(HealthScript))]
    3. public PlayerClassScript : MonoBehaviour {
    4.  
    5.     public enum classes
    6.     {
    7.         Warrior,
    8.         Mage,
    9.         Rogue,
    10.         Paladin
    11.     }
    12.  
    13.     public classes playerClass = classes.Paladin;
    14.  
    15.     void Start()
    16.     {
    17.         HealthScript hs = GetComponent<HealthScript>();
    18.  
    19.         //ClassManager();
    20.         switch(playerClass)
    21.         {
    22.         case classes.Warrior:
    23.             hs.maxHealth = 158.0f;
    24.             break;
    25.         case classes.Mage:
    26.             hs.maxHealth = 100.0f;
    27.             break;
    28.         case classes.Rogue:
    29.             hs.maxHealth = 126.0f;
    30.             break;
    31.         case classes.Paladin:
    32.             hs.maxHealth = 144.0f;
    33.             break;
    34.         }
    35.     }
    36. }
    37.  
     
  8. AndrewGrayGames

    AndrewGrayGames

    Joined:
    Nov 19, 2009
    Posts:
    3,822
    First, let's talk some conventions -

    Enumerations are usually treated as types for most organizations' naming conventions, and enumeration items are usually capitalized, so your...

    Code (csharp):
    1. public enum classes
    2. {
    3.     warrior,
    4.     paladin,
    5.     blackguard,
    6.     // ...SNIP...
    7. }
    ...would in fact be...

    Code (csharp):
    1. public enum Classes
    2. {
    3.     Warrior,
    4.     Paladin,
    5.     Blackguard,
    6.     // SNIP
    7. }
    Next, enumerations are not invoked like a class. This would be the correct way to define one:

    Code (csharp):
    1. public Classes characterClass = Classes.Warrior;
    Finally, your switch statement is generally good, but unless you have a clear default in your enumeration, it's usually a good idea to include a default clause, like so:

    Code (csharp):
    1. switch(characterClass)
    2. {
    3.     case Classes.Warrior:
    4.         // ...Stuff...
    5.         break;
    6.  
    7.     // ...SNIP...
    8.  
    9.     default:
    10.         Debug.LogWarning("This entity has no class!");
    11.         break;
    12. }
    Hope this helps :)

    Note: I know the conventions stuff is a bit subjective, but trust me, if you decide to do this for a day job at some point, it will be helpful to know - you can impress coworkers and mentors by proving you know something! Of course, while the given stuff is convention, there are A) always exceptional organizations that do things differently, and B) exceptional circumstances that would give you good reason to bend these conventions. Hope that helps :)
     
    Last edited: Nov 21, 2013
  9. jolo309

    jolo309

    Joined:
    Mar 4, 2012
    Posts:
    94
    Thank you, that did help some :)