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

Question When an Event returns a class that does not derive from Mono, why isn't it in the Inspector?

Discussion in 'Scripting' started by BenevolenceGames, Apr 14, 2023.

  1. BenevolenceGames

    BenevolenceGames

    Joined:
    Feb 17, 2021
    Posts:
    128
    So I have an event that returns a simple Class, but it won't show in the inspector. If I make the class derive from Monobehavior, it works fine. But, I can not create new instances of Monobehavior with the new keyword, and instead have to add them as components to a gameobject. I don't want this. I want to store data for a future use. Here is the code and a screenshot --

    My Question : How do I have the Unity Button OnClick event pass a class that does not derive from Monobehavior? - OR- What have I done wrong so that mine doesn't show up in the inspector?

    What I tried : I tried changing the CharacterData class so that it derives from Monobehavior, which made it show in the inspector, but caused an error where Monobehviors can not be created with the "new" keyword and I'd need to use GameObject.AddComponent() which is undesired.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [System.Serializable]
    6. public class CharacterData
    7. {
    8.     public string characterName;
    9.     public Color bodyColor;
    10.     public Color faceplateColor;
    11.     public Color backpackColor;
    12.  
    13.     public CharacterData(string characterName, Color bodyColor, Color faceplateColor, Color backpackColor){
    14.         this.characterName = characterName;
    15.         this.bodyColor = bodyColor;
    16.         this.faceplateColor = faceplateColor;
    17.         this.backpackColor = backpackColor;
    18.     }
    19. }
    20.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CharacterCreationForm : MonoBehaviour, InputForm<CharacterData>
    6. {
    7.     public PreviewModel previewModel;
    8.     public TMPro.TMP_InputField nameInput;
    9.     public TMPro.TMP_Dropdown bodyColorSelector;
    10.     public TMPro.TMP_Dropdown faceplateColorSelector;
    11.     public TMPro.TMP_Dropdown backpackColorSelector;
    12.     public GameObject self {get{return this.gameObject;}}
    13.     public event InputForm<CharacterData>.OnClickEvent OnSubmit;
    14.  
    15.     public void OnBodyValueChanged(){
    16.         Color color;
    17.         int value = bodyColorSelector.value;
    18.         switch (value){
    19.             case 0:
    20.                 color = Color.red;
    21.                 break;
    22.             case 1:
    23.                 color = Color.blue;
    24.                 break;
    25.             case 2:
    26.                 color = Color.green;
    27.                 break;
    28.             default:
    29.                 color = Color.black;
    30.                 break;
    31.         }
    32.  
    33.         previewModel.ChangeBodyColor(color);
    34.     }
    35.  
    36.         public void OnFaceplateValueChanged(){
    37.         Color color;
    38.         int value = faceplateColorSelector.value;
    39.         switch (value){
    40.             case 0:
    41.                 color = Color.red;
    42.                 break;
    43.             case 1:
    44.                 color = Color.blue;
    45.                 break;
    46.             case 2:
    47.                 color = Color.green;
    48.                 break;
    49.             default:
    50.                 color = Color.black;
    51.                 break;
    52.         }
    53.  
    54.         previewModel.ChangeFaceplateColor(color);
    55.     }
    56.  
    57.         public void OnBackpackValueChanged(){
    58.         Color color;
    59.         int value = backpackColorSelector.value;
    60.         switch (value){
    61.             case 0:
    62.                 color = Color.red;
    63.                 break;
    64.             case 1:
    65.                 color = Color.blue;
    66.                 break;
    67.             case 2:
    68.                 color = Color.green;
    69.                 break;
    70.             default:
    71.                 color = Color.black;
    72.                 break;
    73.         }
    74.  
    75.         previewModel.ChangeBackpackColor(color);
    76.     }
    77.     public void OnClickSubmit(CharacterData data){
    78.      
    79.         Color bodyColor = previewModel.body.GetComponent<MeshRenderer>().material.color;
    80.         Color faceplateColor = previewModel.facePlate.GetComponent<MeshRenderer>().material.color;
    81.         Color backpackColor = previewModel.backpack.GetComponent<MeshRenderer>().material.color;
    82.         string name = nameInput.text != "" ? nameInput.text : "Player";
    83.  
    84.         data = new CharacterData(name, bodyColor, faceplateColor, backpackColor);
    85.         if (OnSubmit != null){
    86.             OnSubmit(data);
    87.         }
    88.     }
    89.     public void OnClickCancel(){
    90.  
    91.     }
    92. }
    ezgif.com-video-to-gif.gif
     
    Last edited: Apr 14, 2023
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    The reason it doesn't work is OnClickSubmit expects you to pass in a CharacterData object as a parameter. If CharacterData is a MonoBehavior, it thinks you have an instance in the scene that you will drag and drop and attach to the little box that appears.

    In this case, it can't create that instance and have you fill in all the fields within the OnClick event.

    What you may want is possibly a ScriptableObject. Or, even looking at your method. Do you need CharacterData to be a parameter? Within OnClickSubmit you're setting data = new, so you could just create the variable within the method.
     
  3. BenevolenceGames

    BenevolenceGames

    Joined:
    Feb 17, 2021
    Posts:
    128
    It's only passing that arg because the Form interface requires it to pass some T value. I wish it didn't have to. I will have to clear my mind a bit and relook at the solution.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    You could always make a method you can call from Unity's Click event and have that event call the OnClickSubmit?