简体   繁体   中英

Cannot implicitly convert type `UnityEngine.ScriptableObject' to `T'

public class UVBaseTool : ScriptableObject, IPropertiesChanged
{
    private BaseToolProperties properties_;
    private ScriptableObject propertiesNoValidate_;

    // Some code ...

    protected void SetProperties(System.Type propertiesType, System.Type properties1Type)
    {
        properties_ = propertiesType == null ? null : CreateInstance(propertiesType) as BaseToolProperties;
        if (properties_ != null)
        {
            properties_.tool = (this);
        }

        propertiesNoValidate_ = properties1Type == null ? null : CreateInstance(properties1Type);
    }

    public T Properties<T>()
    {
        return properties_;
    }

    public T PropertiesNoValidate<T>()
    {
        return propertiesNoValidate_;
    }
}

Can Any one help me out with this as i am not able to resolve these errors:

  1. Assets/Rocket/Archi/Scripts/Editor/UVEditor/UVBaseTool.cs(219,14): error CS0029: Cannot implicitly convert type Rocket.Archi.BaseToolProperties' to T'
  2. Assets/Rocket/Archi/Scripts/Editor/UVEditor/UVBaseTool.cs(224,14): error CS0029: Cannot implicitly convert type UnityEngine.ScriptableObject' to T'

I tried going through the online context for the CS0029 but still i cant seem to convert through IConvertable. Please let me know if there is a solution.

This is how i am calling to

[SerializeField]
private BaseToolProperties properties_;
[SerializeField]
private ScriptableObject propertiesNoValidate_;

public virtual void Start()
{
      EditorUtil.HideGlobalGizmo();
      ArchiEx.selectedElements.ClearVertexBoxes();
      ArchiEx.currentCamera = (Camera) null;
      GizmoHandler.Destroy();
      if (properties_ != null)
        EditorUtil.LoadObject(properties_, null);
      if (propertiesNoValidate_ != null)
        EditorUtil.LoadObject(propertiesNoValidate_, null);
      Ruler.Add(this, RulerType.Local);
      ArchiSettings.GatherArchi();
}

And this is the baseTool;

using System;
using UnityEngine;

namespace Rocket.Archi
{
  [Serializable]
  public class BaseToolProperties : ScriptableObject
  {
    [NonSerialized]
    public IPropertiesChanged tool;

    public virtual void OnValidate()
    {
      if (tool == null)
        return;
      tool.OnPropertiesChanged();
    }
  }
}

The Image Attached shows how i am calling Properties in other Classes and believe me there is no error in that part i have checked all the context again and again, but this generic function is killing me.

The error Goes away if i write it like this but now i am getting another error which is for IConvertable. When ever i try to make an object in scene view i get this recurring error. See Below;

public T Properties<T>()
    {
        return (T)Convert.ChangeType(properties_, typeof(T));
    }

    public T PropertiesNoValidate<T>()
    {
        return (T)Convert.ChangeType(propertiesNoValidate_, typeof(T));
    }

InvalidCastException: Object must implement IConvertible. System.Convert.ChangeType (System.Object value, System.Type conversionType, System.IFormatProvider provider) (at :0) System.Convert.ChangeType (System.Object value, System.Type conversionType) (at :0) RocketMod.Archi.BaseTool.Properties[T] () (at Assets/Rocket - Modelling Tools/Archi/Scripts/Editor/BaseTool.cs:224) RocketMod.Archi.PropertiesGUI.HasContents () (at Assets/Rocket - Modelling Tools/Archi/Scripts/Editor/PropertiesGUI.cs:54) RocketMod.Archi.PropertiesGUI.OnGUI () (at Assets/Rocket - Modelling Tools/Archi/Scripts/Editor/PropertiesGUI.cs:17) RocketMod.Archi.ToolSelectionGUI.OnGUI () (at Assets/Rocket - Modelling Tools/Archi/Scripts/Editor/ToolSelectionGUI.cs:164) RocketMod.Archi.ArchiEditor.OnInspectorGUI () (at Assets/Rocket - Modelling Tools/Archi/Scripts/Editor/ArchiEditor.cs:299) UnityEditor.InspectorWindow.DrawEditor (UnityEditor.Editor[] editors, System.Int32 editorIndex, System.Boolean rebuildOptimizedGUIBlock, System.Boolean& showImportedObjectBarNext, UnityEngine.Rect & importedObjectBarRect) (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:1253) UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)

if to solve this i write as below everything stops working;

public T Properties<T>() where T : IConvertible
    {
        return (T)Convert.ChangeType(properties_, typeof(T));
    }

    public T PropertiesNoValidate<T>() where T : IConvertible
    {
        return (T)Convert.ChangeType(propertiesNoValidate_, typeof(T));
    }

ANY HELP WOULD BE MUCH APPRECIATED GUYS !!!

You are using generic type <T> in your method that returns a specific typed property.

Replace the signature of your methods by theses :

//     v----------------v--------------The type of properties_
public BaseToolProperties Properties()
{   //                              ^----------------the <T> was removed
    return properties_;
}

//     v--------------v----------------The type of propertiesNoValidate_
public ScriptableObject PropertiesNoValidate()
{   //                                      ^--------the <T> was removed
    return propertiesNoValidate_;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM