Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Tutorial out of date? Can't add elements to this list.

Discussion in 'Scripting' started by Rehtael, Nov 17, 2017.

  1. Rehtael

    Rehtael

    Joined:
    Jun 20, 2017
    Posts:
    53
    I've been following this tutorial to the letter (minus some of the variable names) and I'm getting errors.

    https://unity3d.com/learn/tutorials...ripting/lists-and-dictionaries?playlist=17117

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5.  
    6. public class Character : IComparable<Character>
    7. {
    8.  
    9.     public string name;
    10.     public int classNumber;
    11.  
    12.     public Character(string newName, int newClassNumber)
    13.     {
    14.  
    15.         name = newName;
    16.         classNumber = newClassNumber;
    17.  
    18.     }
    19.  
    20.     public int CompareTo(Character other)
    21.     {
    22.  
    23.         if(other == null)
    24.         {
    25.             return 1;
    26.         }
    27.  
    28.         return classNumber - other.classNumber;
    29.     }
    30.    
    31. }
    32.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CharacterListPersistent : MonoBehaviour
    6. {
    7.    
    8.  
    9.     void Start ()
    10.     {
    11.         List<CharacterInfo> friendlyCharacters = new List<CharacterInfo>();
    12.  
    13.         friendlyCharacters.Add( new CharacterInfo("Chungus", 0));
    14.         friendlyCharacters.Add( new CharacterInfo("Arhok", 0));
    15.  
    16.        
    17.  
    18.     }
    19.    
    20.  
    21.    
    22. }
    23.  
    "Character info does not contain a constructor that takes two arguments"
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Since it's not complaining about the existence of a type named CharacterInfo, I assume you've got a class named Character (which you posted) and a class named CharacterInfo (which you did not post).

    In your second snippet you use CharacterInfo, looks like you wanted to use Character there. At least the character class does provide a contructor that's got a matching parameter list.
     
  3. Rehtael

    Rehtael

    Joined:
    Jun 20, 2017
    Posts:
    53
    Thanks. I was following the same template as the tutorial, but I'm aware it's a few years old now, so I wonder if it's changed up some.
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Well, as far as I can tell from a quick look at the link you provided, it's only about some C# basics. That hasn't changed though. It's just that you used the wrong type's constructor. :)
     
  5. Rehtael

    Rehtael

    Joined:
    Jun 20, 2017
    Posts:
    53
    mweh??? Where? I don't see what the difference is within mine and the tutorial. Maybe it's something obvious right in front of my eyes, but I can't see it.
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Your type
    Code (csharp):
    1. public class Character
    its constructor
    Code (csharp):
    1. public Character(string newName, int newClassNumber)
    but you use CharacterInfo, which is (apparently) another type in your project.

    Code (csharp):
    1. friendlyCharacters.Add( new CharacterInfo("Chungus", 0));
    Either you want to use

    Code (csharp):
    1. friendlyCharacters.Add( new Character("Chungus", 0));
    or you have to add the constructor to the CharacterInfo class.
     
    Rehtael, Munchy2007 and TaleOf4Gamers like this.
  7. Rehtael

    Rehtael

    Joined:
    Jun 20, 2017
    Posts:
    53
    Aaaaaaaaaaah thank you so much.
     
  8. Rehtael

    Rehtael

    Joined:
    Jun 20, 2017
    Posts:
    53
    I think I realized what happened, is I made the script file and named it "Character" but felt like that wasn't specific enough, so I changed it to CharacterInfo and might have missed some cleanup.