Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

[HELP] Issue with Casting Scriptable Objects

Discussion in 'Scripting' started by mschoenhals, Apr 17, 2019.

  1. mschoenhals

    mschoenhals

    Joined:
    Dec 18, 2016
    Posts:
    5
    Hi all,
    I'm a beginner coder trying to work through an issue with changing a value of an array that contains ScriptableObjects:
    Code (CSharp):
    1.         public void AddNewLevel(ScriptableObject newLevel)
    2.         {
    3.  
    4.             var index = Array.IndexOf(abilities, "SH_L1");
    5.  
    6.             abilities[index] = (ScriptableObject)newLevel;
    7.  
    8.         }
    The issue is with the line: abilities[index] = (ScriptableObject)newLevel;

    That's creating an explicit conversion error. I thought that the (ScriptableObject) would let Unity know what the variable is, but I'm wrong.

    Any suggestions?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,305
    'newLevel' is already typed as a ScriptableObject, you shouldn't have to cast it with (ScriptableObject)newLevel.

    What is the exception you're getting?

    What type is the abilities array?

    I have a feeling the exception is actually to do with a type mismatch between the abilities array and ScriptableObject. Especially considering that you have this line:
    Code (csharp):
    1. var index = Array.IndexOf(abilities, "SH_L1");
    Which implies 'abilities' is a string[] array.
     
  3. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    593
    What is abilities an array of? If it was scriptablesobjects then it would be a redundant cast,
     
  4. mschoenhals

    mschoenhals

    Joined:
    Dec 18, 2016
    Posts:
    5
    This is what I'm using to define abilities:
    Code (CSharp):
    1.         [SerializeField] AbilityConfig[] abilities;
    This is my code for creating the array:
    Code (CSharp):
    1.         void AttachInitialAbilities()
    2.         {
    3.             for (int abilityIndex = 0; abilityIndex < abilities.Length; abilityIndex++)
    4.             {
    5.                 abilities[abilityIndex].AttachAbilityTo(gameObject);
    6.             }
    7.         }
    And this is the error:
    error CS0266: Cannot implicitly convert type `UnityEngine.ScriptableObject' to `RPG.Characters.AbilityConfig'. An explicit conversion exists (are you missing a cast?)
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,305
    Yes, your array is of type AbilityConfig, your newLevel is of type 'ScriptableObject'.

    Just like the exception says, it cannot implicitly convert one to the other.

    Does 'AbilityConfig' inherit from 'ScriptableObject' or something? Why is your AddNewLevel taking in a ScriptableObject as a parameter if you expect to stick it in an array of 'AbilityConfig'?
     
  6. mschoenhals

    mschoenhals

    Joined:
    Dec 18, 2016
    Posts:
    5
    "SH_L1" is a scriptableObject within the array
     
  7. mschoenhals

    mschoenhals

    Joined:
    Dec 18, 2016
    Posts:
    5
    yes, AbilityConfig inherits from ScriptableObject. The array has three ScriptableObjects in it.
     
  8. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    290
    It will not find and object in array by it's name automagically. You need to iterate through the array and check every object if it's name is "SH_L1" or whatever you're looking for until it's found or array ended.
     
  9. mschoenhals

    mschoenhals

    Joined:
    Dec 18, 2016
    Posts:
    5
    Ok, I'll have to look up how to do that. Or, would it be better to find it based on it's order in the array? Like this:
    Code (CSharp):
    1. abilities[2] = newLevel;
    The particular ScriptableObject is always in that position in the array.