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. Dismiss Notice

Struct problem

Discussion in 'Scripting' started by a4chteam, Apr 10, 2021.

  1. a4chteam

    a4chteam

    Joined:
    Dec 22, 2020
    Posts:
    19
    Hi, I am writing a dialogue opportunity and ran into a problem. Need help solving.

    Code (CSharp):
    1. [System.Serializable]
    2. public class Dialogue
    3. {
    4.     [System.Serializable]
    5.     public struct Content
    6.     {
    7.         public string Name;
    8.         public Sprite Visual;
    9.         public enum VisualSide {Right, Left};
    10.         public VisualSide Side;
    11.  
    12.         [TextArea(3,10)]
    13.         public string Phrase;
    14.     }
    15.  
    16.     public Content[] Dialogues;
    17. }
    Code (CSharp):
    1. public class DialogueManager : MonoBehaviour
    2. {
    3.     public GameObject DialogueBox;
    4.     public GameObject LeftIcon, RightIcon;
    5.     public Text PhraseBox;
    6.     public Text NameBox;
    7.  
    8.     public Queue<string> ListOfNames;
    9.    
    10.     void Start()
    11.     {
    12.         if(ListOfNames == null)
    13.         {
    14.             ListOfNames = new Queue<string>();
    15.         }
    16.     }
    17.  
    18.     public void StartDialogue(Dialogue Sentences)
    19.     {
    20.         foreach (string c_Name in Sentences.Dialogues.Name)
    21.         {
    22.             ListOfNames.Enqueue(c_Name);
    23.         }
    24.     }
    25. }
    Assets\Scripts\DialogueManager\DialogueManager.cs(25,55): error CS1061: 'Dialogue.Content[]' does not contain a definition for 'Name' and no accessible extension method 'Name' accepting a first argument of type 'Dialogue.Content[]' could be found (are you missing a using directive or an assembly reference?)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Dialogues is an array of Content objects.

    To iterate it properly, use something like:

    Code (csharp):
    1. foreach( var dialog in Dialogues)
    2. {
    3.    /// get at the name here with:
    4.    Debug.Log( dialog.Name);
    5. }
     
    a4chteam likes this.
  3. a4chteam

    a4chteam

    Joined:
    Dec 22, 2020
    Posts:
    19
    Oh, Thanks! Its work!
     
    Kurt-Dekker likes this.
  4. a4chteam

    a4chteam

    Joined:
    Dec 22, 2020
    Posts:
    19
    I'm sorry, but how can you create a queue from an enum?
    I have not found an answer on the net.
     
  5. a4chteam

    a4chteam

    Joined:
    Dec 22, 2020
    Posts:
    19
    Ju! I understood! Thank you!
    Code (CSharp):
    1. public Queue<Dialogue.Content.VisualSide> ListOfSides;
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    This question is apparently nothing to do with the above code?

    You can use System.Enum.GetValues() to list what's in an enum type, like so:

    Code (csharp):
    1. foreach( var keycode in System.Enum.GetValues(typeof(KeyCode)))
    2. {
    3.   // use keycode here
    4. }
     
    a4chteam likes this.
  7. a4chteam

    a4chteam

    Joined:
    Dec 22, 2020
    Posts:
    19
    Wow, that's great. Thank you. I'll remember this!