Newbie question about classes

Hello everyone,

Say I have the code

using UnityEngine;
using System.Collections;

public class test : MonoBehaviour {
public class AnotherClass
{
public int banana;
public void FruitMachine(int a, int b) {}
}
}

I can, within the same script, just write

AnotherClass fruit = new AnotherClass();

But what if I want to do this from another script in unity? The same code will fail, as the type AnotherClass is not known outside of the test.cs script.

Try test.AnotherClass fruit = new test.AnotherClass();

http://www.dotnetperls.com/nested-class

As simple as that? Does that means that, as long as the script test.cs is saved in my project folder, I can, in any other script, do something like

test whatevername = new test();

as well? I’ll go read your link now.

Don’t nest it in a class just make AnotherClass its own public class which will let you reference it anywhere in the main namespace

Yes. You can create a new instance of ‘test’ by calling test myTest = new test();
To call a class inside a class you need to use the syntax I provided you with in my earlier post.

Also like @passerbycmc said, why are you looking to nest the class?

No particular reason, this is just an example from my personal notes where I try to get how C# works. I thank you both for your answer :slight_smile:

If all you want is to put two classes in the same file, you don’t need to nest one inside the other. Instead of this:

public class test : MonoBehaviour {
  public class AnotherClass
  { 
    //stuff......
  }
}

Do this:

public class test : MonoBehaviour {
  //stuff...
}

public class AnotherClass
{ 
  //stuff......
}

What about the name of the file then? I thought the name of the file must be the name of the class (test.cs for the class test).

That’s a Unity-specific thing. Any MonoBehaviours need to have the same name as the file they’re in. But non-MonoBehaviour classes (like “AnotherClass”) can go wherever you want, even in the same files as a MonoBehaviour.

MonoBehaviours must match file names. Anything else can go anywhere. Often it becomes convenient to put data containers and helper classes at the bottom of the file they relate too.

What if I do not use MonoBehaviour at all? Can then the name of the file be changed to be anything at any moment?

Yes, anything you like. Its good practice to make it something sensible that reflects the file contents, but its technically not required.

One (very) last question: what happens if I have two classes in the file inheriting from MonoBehaviour? What name is the file supposed to have? Maybe it’s just not allowed to do that, right?

Yeah, that’s not allowed. The one that has a different name than the file will give you errors and you won’t be able to attach it by dragging the script file onto a game object. Each MonoBehaviour needs to be in its own file with the same name as the class. Anything that’s not a MonoBehaviour can be anywhere you like, including in the same files with a MonoBehaviour.

That’s very clear, thanks for the reply :slight_smile: