Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  3. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    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. 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:
      284
  5. 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 by a moderator: Oct 27, 2021
  6. shoo

    shoo

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