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

Resolved [By design] (IN-18054) Partial MonoBehaviour not detected as MonoBehaviour in 2022.2.0b8

Discussion in '2022.2 Beta' started by Anthiese, Sep 28, 2022.

  1. Anthiese

    Anthiese

    Joined:
    Oct 13, 2013
    Posts:
    73
    Up to 2021.3, I've been able to define MonoBehaviour derived types as partial classes. In 2022.2.0b8, this doesn't work if there are multiple files with parts of the partial class in question. The Inspector view for all the C# files containing parts of that class warn that there's no MonoBehaviour in the class (despite naming the files the same as the class), the class doesn't show up in the Add Component menu, and existing projects with that component in prefabs treat the prefab as missing those scripts (prefab's Inspector shows "(Script)" without script name and no fields).

    This will work in isolation:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public partial class ABehaviour : MonoBehaviour
    4. {
    5.     // Start is called before the first frame update
    6.     void Start()
    7.     {
    8.      
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.      
    15.     }
    16. }
    17.  
    but once another file with another part of that class is added, it doesn't anymore:

    Code (CSharp):
    1.  
    2. public partial class ABehaviour
    3. {
    4. }
    5.  
    I haven't been able to find any information on whether this kind of thing is not going to be supported or if it's some regression. That would be good to know.
     
    Last edited: Oct 2, 2022
  2. RunninglVlan

    RunninglVlan

    Joined:
    Nov 6, 2018
    Posts:
    182
    We have partial MonoBehaviours and as I checked in latest beta (b9) there's no issue with them - they show correctly in Inspector, and it's possible to add them through Add Component
    If it's still reproducible on your end, try adding namespace to your classes, because that's the only difference I see now
     
  3. Anthiese

    Anthiese

    Joined:
    Oct 13, 2013
    Posts:
    73
    The problem comes in if the files are both named identically to the class name. Renaming one of the files makes them work as expected.
     
  4. DreamcatcherProduciton

    DreamcatcherProduciton

    Joined:
    Sep 29, 2016
    Posts:
    32
    I actually appreciate this change as it forces people to explicitly identify partial classes, even if it is a pain that now I have to modify like 50 cs file names :p
     
  5. Neonlyte

    Neonlyte

    Joined:
    Oct 17, 2013
    Posts:
    516