Search Unity

ScriptableObject + Interface => Type can not be found => Can´t assing script (Editor message)

Discussion in 'Scripting' started by pleasurehouse, Apr 11, 2021.

  1. pleasurehouse

    pleasurehouse

    Joined:
    Oct 15, 2020
    Posts:
    96
    I am trying to implement an interface in ScriptableObject. I have no errors but the Inspetor does not recognize the file.

    It says so. Type can not be found => Can´t assing script ..

    How to make this work?

    This is code:

    Code (CSharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using EntityEvent;
    5. //--------------------------------------------------------
    6. [CreateAssetMenu(fileName = "newEventData", menuName = "Data/Event/Info Data")]
    7. public class SOEventData : ScriptableObject, IEventInfo
    8. {
    9.     public SEventData EventData { set; get; }
    10.  
    11.     public int Health=0;        
    12.     public int Lives=0;          
    13.     public int Damage=0;          
    14.     public int Coin=0;            
    15.     public int Tokens=0;          
    16.     public int Score=0;          
    17.     public string Power="none";  
    18.     public string Weapon="none";  
    19.     public string Armor="none";  
    20.  
    21.     public SOEventData()
    22.     {
    23.         SEventData eventData = new SEventData()
    24.         {
    25.             Health = Health,
    26.             Lives = Lives,  
    27.             Damage = Damage,
    28.             Coin = Coin,    
    29.             Tokens = Tokens,
    30.             Score = Score,  
    31.             Power = Power,  
    32.             Weapon = Weapon,
    33.             Armor = Armor    
    34.         };
    35.  
    36.         EventData = eventData;
    37.     }
    38. }
    39. //--------------------------------------------------------
    40.  
    41.  

    Code iside the asset file

    Code (CSharp):
    1.  
    2.  
    3. %YAML 1.1
    4. %TAG !u! tag:unity3d.com,2011:
    5. --- !u!114 &11400000
    6. MonoBehaviour:
    7.   m_ObjectHideFlags: 0
    8.   m_CorrespondingSourceObject: {fileID: 0}
    9.   m_PrefabInstance: {fileID: 0}
    10.   m_PrefabAsset: {fileID: 0}
    11.   m_GameObject: {fileID: 0}
    12.   m_Enabled: 1
    13.   m_EditorHideFlags: 0
    14.   m_Script: {fileID: 0}
    15.   m_Name: EventData
    16.   m_EditorClassIdentifier: [B]Assembly-CSharp[/B]::SOEventData
    17.   Health: 0
    18.   Lives: 0
    19.   Damage: 0
    20.   Coin: 0
    21.   Tokens: 0
    22.   Score: 0
    23.   Power: none
    24.   Weapon: none
    25.   Armor: none
    26.  
    27.  

    In files that normally work the class name is never preceded by Assembly-CSharp


    Code (CSharp):
    1.  
    2. m_EditorClassIdentifier:[B] Assembly-CSharp[/B]::SOEventData
    3.  
    I think that's the problem but I don't know how to fix it. A little help please !!
    Thank you!!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    For Unity types like MonoBehaviors and ScriptableObjects you generally have to stay away from constructors, and you definitely can absolutely NOT make fresh ones using
    new
    .

    To make a MonoBehavior instance in code you must have a GameObject and call AddComponent<T>();

    To make a ScriptableObject instance in code you can use ScriptableObject.CreateInstance<T>();

    You can implement a void Reset() method for use in Editor however (has no effect at runtime!!), which is always better than providing field initializers as far as reducing lifetime confusion when interacting with Unity serialization.
     
    pleasurehouse likes this.
  3. pleasurehouse

    pleasurehouse

    Joined:
    Oct 15, 2020
    Posts:
    96
    Ok, i wil!! thank you so much!!
     
    Kurt-Dekker likes this.