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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

MonoScript has null at GetClass when using record

Discussion in '2021.2 Beta' started by shoo, Oct 23, 2021.

  1. shoo

    shoo

    Joined:
    Nov 19, 2012
    Posts:
    67
    Hello! My root entity at script is record, so when I am trying to get class from MonoScript it is always null. Idk is it by intension, but without it I couldn identify those
     
    Last edited: Oct 23, 2021
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,041
    It's working for me.
    In the
    Editor
    folder:
    TestMonoScript.cs

    Code (CSharp):
    1. using JustToShow;
    2. using UnityEditor;
    3. using UnityEngine;
    4.  
    5. public class TestMonoScript : MonoBehaviour
    6. {
    7.     [MenuItem("Test/MonoScript")]
    8.     private static void TestMS()
    9.     {
    10.         Debug.Log(MonoScript.FromMonoBehaviour(FindObjectOfType<Test>()).GetClass());
    11.     }
    12. }
    Not in an
    Editor
    folder
    Test.cs
    :
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace JustToShow
    4. {
    5.     public class Test : MonoBehaviour {}
    6. }
    I have a game object in the scene which has the
    Test
    component on it.
    I have the
    Test/MonoScript
    menu in the editor, hitting the menu-point:
    screenshot.png

    Are you sure you do not have errors in the console? And/or using ASMDEF?
     
  3. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    679
    MonoScript only works for MonoBehaviours and ScriptableObjects, all other classes aren't considered MonoScripts
     
  4. shoo

    shoo

    Joined:
    Nov 19, 2012
    Posts:
    67
    I think it needs better explanation. I am alredy working with MonoScript in my project. But now, in Unity 21.2 I am trying to use records instead of classes, and can't get any link from MonoScript object to my record which placed inside MonoScript. Records it a new feature of C# so maybe it need to be handled somehow inside MonoScriprt. I have added screenshot!
     

    Attached Files:

    • 11.png
      11.png
      File size:
      29.9 KB
      Views:
      260
  5. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,041
    https://docs.unity3d.com/2021.2/Documentation/Manual/CSharpCompiler.html
    Records are officially unsupported feature.

    Edit: although I have to add, on Windows, purely using records seems to be working and building without error message. But since Unity stated that they don't support them, it is very risky to use them.
     
    Last edited: Oct 27, 2021
  6. shoo

    shoo

    Joined:
    Nov 19, 2012
    Posts:
    67
    I see now, thanks! Tho that's sad :(