Search Unity

Newbie question about classes

Discussion in 'Scripting' started by Thomas12, Mar 2, 2015.

  1. Thomas12

    Thomas12

    Joined:
    Dec 24, 2014
    Posts:
    36
    Hello everyone,


    Say I have the code
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class test : MonoBehaviour {
    5. public class AnotherClass
    6. {
    7. public int banana;
    8. public void FruitMachine(int a, int b) {}
    9. }
    10. }
    I can, within the same script, just write

    Code (CSharp):
    1. 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.
     
  2. Deleted User

    Deleted User

    Guest

  3. Thomas12

    Thomas12

    Joined:
    Dec 24, 2014
    Posts:
    36
    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.
     
  4. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    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
     
  5. Deleted User

    Deleted User

    Guest

    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?
     
  6. Thomas12

    Thomas12

    Joined:
    Dec 24, 2014
    Posts:
    36
    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 :)
     
  7. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    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:
    Code (csharp):
    1. public class test : MonoBehaviour {
    2.   public class AnotherClass
    3.   {
    4.     //stuff......
    5.   }
    6. }
    Do this:
    Code (csharp):
    1. public class test : MonoBehaviour {
    2.   //stuff...
    3. }
    4.  
    5. public class AnotherClass
    6. {
    7.   //stuff......
    8. }
     
  8. Thomas12

    Thomas12

    Joined:
    Dec 24, 2014
    Posts:
    36
    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).
     
  9. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    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.
     
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
  11. Thomas12

    Thomas12

    Joined:
    Dec 24, 2014
    Posts:
    36
    What if I do not use MonoBehaviour at all? Can then the name of the file be changed to be anything at any moment?
     
  12. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Yes, anything you like. Its good practice to make it something sensible that reflects the file contents, but its technically not required.
     
  13. Thomas12

    Thomas12

    Joined:
    Dec 24, 2014
    Posts:
    36
    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?
     
  14. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    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.
     
  15. Thomas12

    Thomas12

    Joined:
    Dec 24, 2014
    Posts:
    36
    That's very clear, thanks for the reply :)