Classes? Custom functions?

I’m new to Unity, but am very impressed with the art pipeline. I was able to create a little game in only two days, surprising myself. Coming from a background in C++ though I’m having some confusion with the Unity system of scripting. I’m wondering how to define a class or struct (or equivalent) with custom functions and data objects etc.?

Thank you very much,
Will

Welcome!

In C# (which you will probably be more comfortable with coming from C++) you get an example of a class when you create a new C# script file. One thing to remeber about unity scripting, if your class uses any unity classes (I think this applies to all) your class must inherit from MonoBehaviour:

public class MyClass : MonoBehaviour 
{

}

If your class doesn’t use any unity-specific functions though, you can inherit from whatever you want, or nothing at all. You can also create a base script that inherits from monobehavior and any subclass of that new script will also have access to monobehaviour. I used this with around 5 seperate classes to create a state machine and steering behaviour AI. Your classes can also be seperate script files, and unity will automaticcaly use them if you reference it (call a function in it or subclass, etc) so you don’t need to have every script on a gameobject. That is really cool. I will throw some simple code here for you in C# to look at, basic stuff but it may help.

using UnityEngine;
using System.Collections;

public class MyBaseClass : MonoBehaviour
{
  
 public void CheckWorld ()
 {
  //blah blah....blah
 }
}

public class MyNewClass : MyBaseClass
{
 /*you can now access the CheckWorld() function in the other class and any Unity function in this class.  Note this class could also be in another file than the top one. */ 
}

I hope that helps, also check out the script wiki:
http://unify.bluegillweb.com/scriptwiki/index.php/Main_Page to see some examples.

Good luck, and feel free to correct me guys if I missed something as I’m no C# expert yet. :wink:

Oh, BTW, AFAIK structs have the same format in C# as in C++. You will want to check to be sure though.

-Jeremy

Ahh, great!

Thank you!

Glad it helped. One thing I forgot to mention which may be a common “gotcha” if you are used to OOP in C++ is that in C# there is no multiple class inheritance. A class can only inherit from one class. There are other ways of getting access to multiple classes though, you just can’t sublass more than one. There are Interfaces in C# which are what they sound like, they are to set up your class Interfaces (saves your butt, if you forget to implement a required method, it will tell you), and AFAIK you can inherit from as many Interfaces as you like.

A good tutorial on C# is here: About US | Award-Winning Software Development Company

Good luck,
-Jeremy

Wanted to clarify this works for files, which is what I needed. *Changing the base code to my real world Math Helper Clamper. Basically takes a value, the min, and the max, and locks it into those values. NICE for Health going from 0 - 100% in this example.

__'Clamp.cs'__
using UnityEngine;
using System.Collections;

public class Clamp:  MonoBehaviour
{		
    public static int Clamp(int value, int min, int max)
    {
          return (value < min) ? min : (value > max) ? max : value;
    }
}
__'PlayerHealth.cs'__
using UnityEngine;
using System.Collections;

public class PlayerHealth :  Clamp
{
	public int MaxHealth = 100;
	public int CurHealth = 100;
	public float HealthBarLength;
	public float HealthBarMaxLength;
	
	public void AdjustCurrentHealth(int Adj)
	{
		CurHealth += Adj;
	                CurHealth = Clamp(CurHealth, 0, MaxHealth ); 
	}
}

I gotta reply this is exactly what I was looking for. Its brillant. I was trying to come up with some base functions for my scripts. This made it a quick and dirty way to basically put together a ‘DLL type’ of object. That I can call from different scripts, all wrapped up in a base class that I have all the ‘functions’ stored in one place.

I guess the basic functionality is there. Begs the question. Would you eventually want to go into a DLL for these types of base functions you need for your scripts. Helper functions if you will?

Mathf.Clamp already exists; no need to reinvent it.

–Eric