C# Noob Question / Custom Classes

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 ? :slight_smile:

SCRIPT NOT IN THE SCENE

using UnityEngine;
using System.Collections;

//Doesnt work if public class Ammo : MonoBehaviour {

[System.Serializable]
public class Ammo  {
  
    public int Ammo_Shotgun = 10;
    public int Ammo_Gun =10;
  
}

SCRIPT CALLING THE CUSTOM PUBLIC CLASS AMMO

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

    public Ammo[] myAmmo;

    // Use this for initialization
    void Start () {
  
        myAmmo =  new Ammo[10];

    }
  
    // Update is called once per frame
    void Update () {
  
        if(Input.GetKeyDown("space")){
            myAmmo[0].Ammo_Gun --;
       

        }

    }
}

Thanks a lot !

if it’s a monobehaviour, then it’s considered a component, and must be added to an object (not new 'd) with AddComponent()
But you shouldn’t have more than one on the same object (usually)

1 Like

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

1 Like

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

Check out my signature for a link to some tutorials. In my intermediate/advanced series I talk about custom classes.

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.