Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

type mismatch

Discussion in 'Scripting' started by jister, May 30, 2013.

  1. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    this results in a list filled with mismatches that go missing on play?
    KeyItem derives from Item which derives from scriptableobject, ah and Element also derives from keyitem. all these are Serialized.
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Text.RegularExpressions;
    5.  
    6. [System.Serializable]
    7. public class Molecule : KeyItem
    8. {
    9.     public List<Element> moleculeElements = new List<Element>();
    10.    
    11.     public Molecule ()
    12.     {
    13.         moleculeElements = new List<Element>();
    14.     }
    15.     public Molecule (string name)
    16.     {
    17.         ReadMolecule(name);
    18.     }
    19.     private void ReadMolecule (string name)
    20.     {  
    21.     var matches ="([A-Z][a-z]?)";
    22.     string[] output= Regex.Split(name, matches);
    23.     for(int i=1; i<output.Length-1; i+=2)
    24.     {  
    25.         Element element= ScriptableObject.CreateInstance<Element>() as Element;
    26.         element=new Element(output[i]);
    27.         moleculeElements.Add(element);
    28.     }
    29.  
    30.     }
    31. }
     
    Last edited: May 30, 2013
  2. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    nobody :(
    any suggestion is welcome atm... I tried everything i cloud think of and didn't find any solution on the net... well if i only knew what causes it i could at least do a better search

    my script currently
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Text.RegularExpressions;
    5.  
    6. [System.Serializable]
    7. public class Molecule : KeyItem
    8. {
    9.     [SerializeField]
    10.     private string _notation;
    11.     [SerializeField]
    12.     private List<Element> _moleculeElements = new List<Element>();
    13.    
    14.     public Molecule ()
    15.     {
    16.         _moleculeElements = new List<Element>();
    17.     }
    18.     public Molecule (string notation)
    19.     {
    20.         this._notation=notation;
    21.         ReadMolecule(_notation);
    22.     }
    23.    
    24.     public string Notation
    25.     {
    26.         get{return _notation;}
    27.         set{_notation = value;}
    28.     }
    29.     public List<Element> MoleculeElements
    30.     {
    31.         get{return _moleculeElements;}
    32.         set{_moleculeElements = value;}
    33.     }
    34.     private void ReadMolecule (string name)
    35.     {  
    36.     var matches ="([A-Z][a-z]?)";
    37.     string[] output= Regex.Split(name, matches);
    38.     for(int i=1; i<output.Length-1; i++)
    39.     {  
    40.         if(System.Char.IsLetter(output[i],0))
    41.         {
    42.             Element element= ScriptableObject.CreateInstance<Element>() as Element;
    43.             element=new Element(output[i]);
    44.             _moleculeElements.Add(element);
    45.         }
    46.         else if(System.Char.IsDigit(output[i],0))
    47.         {
    48.             _moleculeElements[i-2].Amount=int.Parse(output[i]);
    49.         }
    50.     }
    51.  
    52.     }
    53. }
    54.  
    it is supposed to read a chemical notation of a molecule and create the proper instances of elements the molecule is made of.
    the Elements have a constructor that gets their values from "periodic table", a class with all functions to get the values needed base on the element symbol.
    creating an element as type works fine so i take it that my periodic table and element class work fine,
    the _moleculeElements List shows the mismatch for the elements, but they still have the right values if i double click on them to see an element instance...
    the only difference with creating an element directly is that all items i make are also created as prefabs via asset database, and the ones that a molecule class makes are not
    which is intended... unless if that is the problem...
     
    Last edited: May 30, 2013
  3. goat

    goat

    Joined:
    Aug 24, 2009
    Posts:
    5,182
    It's been a long time but I think were I do that in Unix that would be a 2 to 3 length name but not longer than 3 characters where the first 2 characters are alphabetic, irrespective of case, and the third is blank or any other printable character.

    So you are looking for molecular formulas like Na, N, Ag, but not H2O, CO2 or NaClO4?

    Just trying to help...not a chemist, haven't had a class since in it since 1985...

    The regex hasn't an expression to match the 2 in H2O for example.

    In Unix I'd match NaClO4 and other molecular formulas like this:

    [A-Z][a-z]?|[0-9]+
     
    Last edited: May 30, 2013
  4. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    hey goat,
    i use notations like H2O, the reading of the molecule works find i get the elements with their amount back as i want, only the Element instances created in the last for loop malfunction and are in some way not of type Element? or the List says so? or isn't that what type mismatch means? does it mean there is something wrong with the data in the type?
     
  5. goat

    goat

    Joined:
    Aug 24, 2009
    Posts:
    5,182
    Oh, sorry. OK.
    Code (csharp):
    1.  
    2.             _moleculeElements.Add(element);
    3.  
    So line gets data type mismatch.

    Should

    private string _notation;

    be of same type as defined in "Periodic Table" Can you put the error message you get here?
     
  6. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    this is the periodic table class
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class PeriodicTable
    4. {
    5.     [SerializeField]
    6.     private Element _thisElement;
    7.    
    8.     public PeriodicTable (string symbol)
    9.     {
    10.         _thisElement = (Element)ScriptableObject.CreateInstance<Element>();
    11.         _thisElement = new Element(symbol);
    12.         _thisElement.Symbol=symbol;
    13.         //_element.ID=id;
    14.     }
    15.     public PeriodicTable (Element element)
    16.     {
    17.         this._thisElement=element;
    18.         ValenceElectronsBySymbol(element.Symbol);
    19.         AtomicNumberBySymbol(element.Symbol);
    20.     }
    21.    
    22.     public Element ThisElement
    23.     {
    24.         get{return _thisElement;}
    25.         set{_thisElement = value;}
    26.     }
    27.    
    28.     private void ValenceElectronsBySymbol (string symbol)
    29.     {      
    30.         int e=1;
    31.         string[] collums=new string[] {"H","Li","Na","K","Rb","Cs","Fr",
    32.         "-","Be","Mg","Ca","Sr","Ba","Ra","-","B","-","C","Si",
    33.         "-","N","P","As","-","O","S","Se","Te","-","F","Cl",
    34.         "Br","I","At","-","He","Ne","Ar","Kr","Xe","Rn"};
    35.        
    36.         for(var j=0; j<collums.Length; j++)
    37.         {
    38.             if(collums[j]=="-")e++;
    39.             if(symbol==collums[j])
    40.             {
    41.                 _thisElement.ValenceElectrons=e;
    42.                 _thisElement.NonMetal=true;
    43.             }
    44.         }
    45.        
    46.     }
    47.     private void AtomicNumberBySymbol (string symbol)
    48.     {
    49.         int c=0;
    50.         int r=0;
    51.         string[] collums=new string[]
    52.         {
    53.             "-","H","Li","Na","K","Rb","Cs","Fr",
    54.             "-","Be","Mg","Ca","Sr","Ba","Ra",
    55.             "-","Sc","Y","La","Ac",
    56.             "-","Ti","Zr","Hf","Rf",
    57.             "-","V","Nb","Ta","Db",
    58.             "-","Cr","Mo","W","Sg",
    59.             "-","Mn","Tc","Re","Bh",
    60.             "-","Fe","Ru","Os","Hs",
    61.             "-","Co","Rh","Ir","Mt",
    62.             "-","Ni","Pd","Pt","Ds",
    63.             "-","Cu","Ag","Au","Rg",
    64.             "-","Zn","Cd","Hg","Cn",
    65.             "-","B","Al","Ga","In","Tl","Uut",
    66.             "-","C","Si","Ge","Sn","Pb","Fl",
    67.             "-","N","P","As","Sb","Bi","Uup",
    68.             "-","O","S","Se","Te","Po","Lv",
    69.             "-","F","Cl","Br","I","At","Uus",
    70.             "-","He","Ne","Ar","Kr","Xe","Rn","Uuo"
    71.         };
    72.         for(int i=0; i<collums.Length; i++)
    73.         {
    74.             if(collums[i]=="-")c++;
    75.             if(symbol==collums[i])Debug.Log (c);
    76.         }
    77.         string[] rows=new string[]
    78.         {
    79.             "-","H","He",
    80.             "-","Li","Be","B","C","N","O","F","Ne",
    81.             "-","Na","Mg","Al","Si","P","S","Cl","Ar",
    82.             "-","K","Ca","Sc","Ti","V","Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Ge","As","Se","Br","Kr",
    83.             "-","Rb","Sr","Y","Zr","Nb","Mo","Tc","Ru","Rh","Pd","Ag","Cd","In","Sn","Sb","Te","I","Xe",
    84.             "-","Cs","Ba","La","Ce","Pr","Nd","Pm","Sm","Eu","Gd","Tb","Dy","Ho","Er","Tm","Yb","Lu",
    85.                 "Hf","Ta","W","Re","Os","Ir","Pt","Au","Hg","Tl","Pb","Bi","Po","At","Rn",
    86.             "-","Fr","Ra","Ac","Th","Pa","U","Np","Pu","Am","Cm","Bk","Cf","Es","Fm","Md","No","Lr",
    87.                 "Rf","Db","Sg","Bh","Hs","Mt","Ds","Rg","Cn","Uut","Fl","Uup","Lv","Uus","Uuo"
    88.         };
    89.         for(int ii=0; ii<collums.Length; ii++)
    90.         {
    91.             if(collums[ii]=="-")r++;
    92.             if(symbol==collums[ii])Debug.Log (r);
    93.         }
    94.         string[] atomNumber=new string[]
    95.         {
    96.             "-","H","He",
    97.             "Li","Be","B","C","N","O","F","Ne",
    98.             "Na","Mg","Al","Si","P","S","Cl","Ar",
    99.             "K","Ca","Sc","Ti","V","Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Ge","As","Se","Br","Kr",
    100.             "Rb","Sr","Y","Zr","Nb","Mo","Tc","Ru","Rh","Pd","Ag","Cd","In","Sn","Sb","Te","I","Xe",
    101.             "Cs","Ba","La","Ce","Pr","Nd","Pm","Sm","Eu","Gd","Tb","Dy","Ho","Er","Tm","Yb","Lu",
    102.                 "Hf","Ta","W","Re","Os","Ir","Pt","Au","Hg","Tl","Pb","Bi","Po","At","Rn",
    103.             "Fr","Ra","Ac","Th","Pa","U","Np","Pu","Am","Cm","Bk","Cf","Es","Fm","Md","No","Lr",
    104.                 "Rf","Db","Sg","Bh","Hs","Mt","Ds","Rg","Cn","Uut","Fl","Uup","Lv","Uus","Uuo"
    105.         };
    106.         for(int n=0; n<atomNumber.Length; n++)
    107.         {
    108.             if(symbol==atomNumber[n])_thisElement.AtomicNumber=n;
    109.         }
    110.     }
    111.    
    112. }
    113.  
    and the Element class:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class Element : KeyItem
    6. {
    7.     [SerializeField]
    8.     private int _atomicNumber;
    9.     [SerializeField]
    10.     private string _symbol;
    11.     [SerializeField]
    12.     private int _valenceElectrons;
    13.     [SerializeField]
    14.     private bool _nonMetal;
    15.     [SerializeField]
    16.     private int _electroNegativity;
    17.     [SerializeField]
    18.     private int _atomicRadius;
    19.     [SerializeField]
    20.     private Color _atomColor;
    21.     [SerializeField]
    22.     private int _capacity;
    23.    
    24.    
    25.     public Element (string symbol)
    26.     {
    27.         this._symbol = symbol;
    28.         new PeriodicTable(this);
    29.     }
    30.    
    31.     #region get/set
    32.     public int AtomicNumber
    33.     {
    34.         get{return _atomicNumber;}
    35.         set{_atomicNumber = value;}
    36.     }
    37.     public string Symbol
    38.     {
    39.         get{return _symbol;}
    40.         set{_symbol = value;}
    41.     }
    42.     public int ValenceElectrons
    43.     {
    44.         get{return _valenceElectrons;}
    45.         set{_valenceElectrons = value;}
    46.     }
    47.     public bool NonMetal
    48.     {
    49.         get{return _nonMetal;}
    50.         set{_nonMetal = value;}
    51.     }
    52.     public int ElectroNegativity
    53.     {
    54.         get{return _electroNegativity;}
    55.         set{_electroNegativity = value;}
    56.     }
    57.     public int AtomicRadius
    58.     {
    59.         get{return _atomicRadius;}
    60.         set{_atomicRadius = value;}
    61.     }
    62.     public Color AtomColor
    63.     {
    64.         get{return _atomColor;}
    65.         set{_atomColor = value;}
    66.     }
    67.     public int Capacity
    68.     {
    69.         get{return _capacity;}
    70.         set{_capacity = value;}
    71.     }
    72.    
    73.     #endregion
    74. }
    75.  
    this is the result:
     
    Last edited: May 31, 2013
  7. goat

    goat

    Joined:
    Aug 24, 2009
    Posts:
    5,182
    OK, so I'm trying to learn C# by helping people with questions until I order a C# book (I know C/C++) so if I'm wrong at least the information will be here for others to help you:

    a. _thisElement = (Element)ScriptableObject.CreateInstance<Element>();

    b. Element element= ScriptableObject.CreateInstance<Element>() as Element;

    and the only thing I can come up is with is the lack of a cast on the later b. code causing the problem, although to me that 1st line of a. code shouldn't need a cast. These List, Dictionary, and Serialization is in particular very foreign to me and the syntax odd too, but that's what I'm trying to learn.
     
  8. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    i tried both casts manners but that didn't change anything... :-(
    only other part involved is this:
    ah yes and i know i shouldn't use a constructor with ScriptableObject will change it to OnEnable() but even if i comment out molecule=new Molecule(notation); I still have mismatch.

    Code (csharp):
    1. case ItemType.molecule:
    2.                         notation = EditorGUILayout.TextField("Chemical Notation : ", notation);
    3.                         if(GUILayout.Button("Create Item", GUILayout.Width(150)))
    4.                         {
    5.                             Molecule molecule=(Molecule)ScriptableObject.CreateInstance<Molecule>();
    6.                             molecule.Notation=notation;
    7.                             molecule = new Molecule(notation);
    8.                             molecule.ItemName=itemName;
    9.                             molecule.Description=description;
    10.                             molecule.ThisItem=itemObj;
    11.                             molecule.Icon="KeyItem";
    12.                             itemManager.itemList.Add(molecule);
    13.                             index = itemManager.itemList.IndexOf(molecule );
    14.                             AssetDatabase.CreateAsset(molecule, "Assets/Items/"+index+"_"+molecule.ItemName+"_molecule.prefab");
    15.                             Create(index);
    16.                         }
    17.                     break;
     
  9. goat

    goat

    Joined:
    Aug 24, 2009
    Posts:
    5,182
    Sorry, my C# just isn't there yet.

    I'd post the actually last few lines of your Unity console complaining, and with this other information, one of the guys that have done C# in the workplace will come by and help out in the next day or so.
     
  10. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    the console trows nothing thats why i have no clue as to where it goes wrong... think I'll try debug the instance creation as close as possible when i have the time,
    thanks for the help
     
  11. goat

    goat

    Joined:
    Aug 24, 2009
    Posts:
    5,182
    So I added the code to my project and I'm missing the KeyItem (-> Item -> ScriptableObject) code to try and learn a bit.

    Maybe you should:

    Element element= (Element)KeyItem.CreateInstance<Element>() as Element;

    or it seems you did all the inheritance to do this to me:

    Element element= new Element(output);

    and leave out the first line ScriptableObject line.

    I'll leave it for an expert to come by now.
     
  12. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    well i changed this : in the molecule class
    Code (csharp):
    1. var matches ="([A-Z][a-z]?)";
    2.     string[] output= Regex.Split(notation, matches);
    3.     for(int i=1; i<output.Length-1; i+=2)
    4.     {  
    5.         string symbol=output[i];
    6.         string amount=output[i+1].ToString();
    7.         if(amount=="")amount="1";
    8.         Element element= (Element)ScriptableObject.CreateInstance<Element>();
    9.         element=new Element(symbol);
    10.         element.Amount=int.Parse(amount);
    11.         _moleculeElements.Add(element);
    12.         AssetDatabase.CreateAsset(element, "Assets/Items/"+notation+"_"+symbol+"_element.prefab");
    13.        
    14.        
    15.     }
    so now it creates a prefab for the instances and now they show up...
    but i wanted to wait with the create until i actually needed them. now i will have a couple of 100 prefabs of instances more in my game :-(
     
  13. goat

    goat

    Joined:
    Aug 24, 2009
    Posts:
    5,182
    OK, now I remember some code I have from a friend that acted similar. In this case an array in the Editor Inspector was initialized to size 10 and Unity complained until each of those 10 array elements had a prefab drag dropped into those slots.

    I'll ask tomorrow if anything done in the new package fixed that behavior and relay it to you.
     
  14. goat

    goat

    Joined:
    Aug 24, 2009
    Posts:
    5,182
    OK, he fixed it by adding a statement like this:

    if (array != null)
    access reference code complaining about type mismatch at runtime...
     
  15. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    hey thanks goat, will look try and let you know if that helped

    just tried it like this:
    Code (csharp):
    1. private void ReadMolecule (string notation)
    2.     {  
    3.     var matches ="([A-Z][a-z]?)";
    4.     string[] output= Regex.Split(notation, matches);
    5.     if(output != null)
    6.     {
    7.         for(int i=1; i<output.Length-1; i+=2)
    8.         {  
    9.             string symbol=output[i];
    10.             string amount=output[i+1].ToString();
    11.             if(amount=="")amount="1";
    12.             Element element= (Element)ScriptableObject.CreateInstance<Element>();
    13.             element=new Element(symbol);
    14.             element.Amount=int.Parse(amount);
    15.             _moleculeElements.Add(element);
    16.             //AssetDatabase.CreateAsset(element, "Assets/Items/"+notation+"_"+symbol+"_element.prefab");
    17.         }
    18.            
    19.     }
    but then the mismatch is back. I find it a strange solution and don't really see how it could fix things.
    anyway if you want i can send you the itemSystem as package in a pm, maybe you can show me how you/he solved it then?
    let me know if you want that...
     
    Last edited: Jun 1, 2013
  16. zackivano

    zackivano

    Joined:
    Mar 20, 2012
    Posts:
    11
    It's a old thread and maybe you found your answer, but I encountered the same issue and I start to learn a bit about ScriptableObject and serialization in Unity to find a solution.
    I wrote a memorandum on how to avoid this issue and some info on serialization. Maybe someone out there would be interested in how achieve a serialization of a list of custom type and multi-list in Unity so here you are: http://ivanozanchetta.com/gamedev/unity3d/unity-serialization-behind-scriptableobject/
    This post is not supposed to be the "holy grail" of serialization, but is the way I found solutions to some issue, so take a read and drop a comment...
     
    T-Zee likes this.