Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Problems with making multiple classes within one script.

Discussion in 'Scripting' started by Draconic, Jun 30, 2014.

  1. Draconic

    Draconic

    Joined:
    Oct 12, 2013
    Posts:
    82
    I have a very small class which is only used for one thing, so I want to put the class in the script that uses it. The script that uses it doesn't derive from monobehaviour and has [System.Serializable].

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. [System.Serializable]
    6.  
    7. public class TextInteraction {
    8.  
    9.     public TextInteraction_Message messages = new TextInteraction_Message();
    10.  
    11.     void Start () {
    12.    
    13.     }
    14.  
    15.     void Update () {
    16.    
    17.     }
    18.  
    19.     public TextInteraction(){
    20.  
    21.     }
    22.  
    23.     public TextInteraction(TextInteraction textInteraction){
    24.  
    25.     }
    26. }
    27.  
    28. public class TextInteraction_Message{
    29.     public enum SendOn {
    30.         FadedOut,
    31.         FadingOut,
    32.         FadedIn,
    33.         FadingIn
    34.     }
    35.     public string message;
    36.     public List<SendOn> SendOns = new List<SendOn> ();
    37.  
    38.     public TextInteraction_Message(){
    39.  
    40.     }
    41. }
    42.  
    This runs fine, but the list of classes within the class aren't shown in the inspector. When I create an instance of TextInteraction, that shows in the inspector as well as any public variables in TextInteraction - Except for the list of TextInteraction_Message. Is there any way I can display the TextInteraction_Message in the inspector?
     
  2. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    [System.Serializable] works only with one class (TextInteraction) below. So mark your TextInteraction_Message class as serializable and everything will work. All nested classes you want to see in inspector must be marked.
     
  3. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,039
    What he said... I think the spacing makes it clear that you aren't sure on how annotations are applied. Annotations are applied to the line below the annotation.

    Annotate a class:
    Code (csharp):
    1.  
    2. [Annotation]
    3. public class MyClass() {}
    4.  
    Annotate a method:
    Code (csharp):
    1.  
    2. [Annotation]
    3. public void MyMethod() {}
    4.  
     
  4. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    ot: Start & Update won't be called since the secondary class doesn't extend to MonoBehaviour o;
     
  5. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    ... declaration immediately following the annotation, right?

    I know that sounds picky, but there are plenty of situations where "the line below" is misleading or incorrect (putting it on the same line, having blank lines, etc.).
     
  6. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,039
    Indeed, although the comment was attempting to clarify between above and below (even though not technically correct, potentially clearer to the OP).
     
  7. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    Sure, but that's an audience that perhaps needs to take a few moments to learn about declarations and white space. ;)