Search Unity

C# Noob Question / Custom Classes

Discussion in 'Scripting' started by bigboybifdick, Jul 3, 2015.

  1. bigboybifdick

    bigboybifdick

    Joined:
    Feb 8, 2014
    Posts:
    60
    Hi guys,

    I'm currently learning C#, i just discovered custom classes...
    I'm not sure why i can access a custom class (see example below) with a custom serialized class, but i can't if the class is derived from Monobehaviour.

    - Can someone explain me why ? Or share link ? :)

    SCRIPT NOT IN THE SCENE
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. //Doesnt work if public class Ammo : MonoBehaviour {
    5.  
    6. [System.Serializable]
    7. public class Ammo  {
    8.  
    9.     public int Ammo_Shotgun = 10;
    10.     public int Ammo_Gun =10;
    11.  
    12. }
    13.  
    SCRIPT CALLING THE CUSTOM PUBLIC CLASS AMMO
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Player : MonoBehaviour {
    5.  
    6.     public Ammo[] myAmmo;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.  
    11.         myAmmo =  new Ammo[10];
    12.  
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update () {
    17.  
    18.         if(Input.GetKeyDown("space")){
    19.             myAmmo[0].Ammo_Gun --;
    20.        
    21.  
    22.         }
    23.  
    24.     }
    25. }
    26.  

    Thanks a lot !
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    if it's a monobehaviour, then it's considered a component, and must be added to an object (not new 'd) with AddComponent<name>()
    But you shouldn't have more than one on the same object (usually)
     
    bigboybifdick likes this.
  3. malkere

    malkere

    Joined:
    Dec 6, 2013
    Posts:
    1,212
    classes don't need to be object oriented to be used.
    data needs to be object oriented or static.
    what you are doing is defining a wrapper, a new class called ammo.
    then, assign data in an object oriented (monobehavior) class to be used.
    that doesn't break any rules.

    if you tried to run something in the Ammo class you would find that impossible because it doesn't exist. The way you've shown you're just leaving it in empty space for other scripts to look at and use the instructions, but never for storage.

    the double = 10's are just default values if an Ammo item is created, again, instructions. they don't actually exist until initialized somewhere.

    and what john said
     
    bigboybifdick likes this.
  4. bigboybifdick

    bigboybifdick

    Joined:
    Feb 8, 2014
    Posts:
    60
    Hey Guys,

    Thanks a lot for the answers.
    If i get it right, the data in custom classes can be read (used as instructions by another script), but cannot be modified or run anything.

    That said, i'm not sure to see when i could need them. Could you tell me when they should be used ?
    The only advantage I see, is that they allow me to store static data. Is there anything else ?

    Thanks again !! <3
     
  5. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Check out my signature for a link to some tutorials. In my intermediate/advanced series I talk about custom classes.
     
  6. malkere

    malkere

    Joined:
    Dec 6, 2013
    Posts:
    1,212
    storing data is a big one. also creating functions accessible to anyone. the biggest is probably for creating types though. also known as wrapping. creating lots of weapons is much easier with a Weapon class instead of trying to fit everything into one ten thousand lined script. Then you can say Weapon sword = new Weapon(name, randomDamage, randomSpeed); etc. and make everything you need for whatever you are doing. Most things have to be variable based so they can change and grow or else everything in your program will be the same. Making types allows you to organize those many different things, and lets the program know what it's dealing with so you can say sword.GetDamage(), and not sword.GetArmor(), monodevelop will know what it is you are dealing with.

    Tutorials are the way to go though! You should go through all the scripting tutorials that are on the unity website, and lots of people offer help with that like SubZeroGaming. If you find something you don't understand wait a month or two and you'll find a reason to watch it again.