Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

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:
      294
  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 :(