Search Unity

The same field name is serialized multiple times in the class or parent class? (error)

Discussion in 'Scripting' started by Calmax, Aug 21, 2019.

  1. Calmax

    Calmax

    Joined:
    Aug 8, 2017
    Posts:
    20
    Hi all,

    I am in the process of creating NPC interactions for quests in my game and have come across this error.

    It is confusing me because I am only using the filed name once in a single script? (I feel I'm wrong about this).

    Any suggestions would be appreciated!

    I have linked the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class NPC : Interactable
    6. {
    7.     private GameObject triggeringNpc;
    8.     private bool triggering;
    9.  
    10.     public string[] dialogue;
    11.     public string name;
    12.     public GameObject npcText;
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         if (triggering)
    18.         {
    19.             npcText.SetActive(true);
    20.  
    21.             if (Input.GetKeyDown(KeyCode.E))
    22.             {
    23.                 DialogueSystem.Instance.AddNewDialogue(dialogue, name);
    24.                 print("Talking to NPC");
    25.                 Cursor.visible = true;
    26.                 Cursor.lockState = CursorLockMode.None;
    27.  
    28.                 GameObject varGameObject = GameObject.FindWithTag("MainCamera");
    29.                 varGameObject.GetComponent<PlayerLook>().enabled = false;
    30.             }
    31.         }
    32.         else
    33.         {
    34.             npcText.SetActive(false);
    35.         }
    36.     }
    37.  
    38.     private void OnTriggerEnter(Collider other)
    39.     {
    40.         if (other.tag == "Player")
    41.         {
    42.             triggering = true;
    43.             triggeringNpc = other.gameObject;
    44.         }
    45.     }
    46.  
    47.     private void OnTriggerExit(Collider other)
    48.     {
    49.         triggering = false;
    50.         triggeringNpc = null;
    51.         Cursor.visible = false;
    52.         Cursor.lockState = CursorLockMode.Locked;
    53.  
    54.         GameObject varGameObject = GameObject.FindWithTag("MainCamera");
    55.         varGameObject.GetComponent<PlayerLook>().enabled = true;
    56.  
    57.     }
    58.  
    59.     public override void Interact()
    60.     {
    61.         DialogueSystem.Instance.AddNewDialogue(dialogue, name);
    62.         Debug.Log("Interacting with NPC");
    63.     }
    64. }
    65.  

    And the other script:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class QuestGiver : NPC
    6. {
    7.     public bool AssignedQuest { get; set; }
    8.     public bool Helped { get; set; }
    9.  
    10.  
    11.     [SerializeField]
    12.     private GameObject quests;
    13.  
    14.     [SerializeField]
    15.     private string questType;
    16.  
    17.     private Quest Quest { get; set; }
    18.  
    19.     public override void Interact()
    20.     {
    21.      
    22.         if (!AssignedQuest && !Helped)
    23.         {
    24.             base.Interact();
    25.             AssignQuest();
    26.         }
    27.         else if(AssignedQuest && !Helped)
    28.         {
    29.             CheckQuest();
    30.         }
    31.         else
    32.         {
    33.             DialogueSystem.Instance.AddNewDialogue(new string[] { "Appreciate you cleaning out the soldier for me!" }, name);
    34.         }
    35.     }
    36.  
    37.     void AssignQuest()
    38.     {
    39.         AssignedQuest = true;
    40.         Quest = (Quest)quests.AddComponent(System.Type.GetType(questType));
    41.     }
    42.  
    43.     void CheckQuest()
    44.     {
    45.         if (Quest.Completed)
    46.         {
    47.             Debug.Log("Player would receive reward here.");
    48.             Helped = true;
    49.             AssignedQuest = false;
    50.             DialogueSystem.Instance.AddNewDialogue(new string[] { "Thanks! Here's your reward!", "More dialogue" }, name );
    51.         }
    52.         else
    53.         {
    54.             DialogueSystem.Instance.AddNewDialogue(new string[] { "Did you get that task done for me?"}, name);
    55.         }
    56.     }
    57. }
    58.  
    And the final block:

    Code (CSharp):
    1. public class QuestGiver : NPC
    2. {
    3.     public bool AssignedQuest { get; set; }
    4.     public bool Helped { get; set; }
    5.  
    6.  
    7.     [SerializeField]
    8.     private GameObject quests;
    9.  
    10.     [SerializeField]
    11.     private string questType;
    12.  
    13.     private Quest Quest { get; set; }
    14.  
    15.     public override void Interact()
    16.     {
    17.      
    18.         if (!AssignedQuest && !Helped)
    19.         {
    20.             base.Interact();
    21.             AssignQuest();
    22.         }
    23.         else if(AssignedQuest && !Helped)
    24.         {
    25.             CheckQuest();
    26.         }
    27.         else
    28.         {
    29.             DialogueSystem.Instance.AddNewDialogue(new string[] { "Appreciate you cleaning out the soldier for me!" }, name);
    30.         }
    31.     }
    32.  
    33.     void AssignQuest()
    34.     {
    35.         AssignedQuest = true;
    36.         Quest = (Quest)quests.AddComponent(System.Type.GetType(questType));
    37.     }
    38.  
    39.     void CheckQuest()
    40.     {
    41.         if (Quest.Completed)
    42.         {
    43.             Debug.Log("Player would receive reward here.");
    44.             Helped = true;
    45.             AssignedQuest = false;
    46.             DialogueSystem.Instance.AddNewDialogue(new string[] { "Thanks! Here's your reward!", "More dialogue" }, name );
    47.         }
    48.         else
    49.         {
    50.             DialogueSystem.Instance.AddNewDialogue(new string[] { "Did you get that task done for me?"}, name);
    51.         }
    52.     }
    53. }
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,702
    What error?

    You show us code, but don't describe the error you're getting.

    Is it your title?
    If so, say that. Maybe show the exception/error in its entirity (they usually include more information then just a message. Like an error code, or exceptions often include the line number).

    I do notice you have a field named "name", not that this is already a property of MonoBehaviour. All types that inherit from UnityEngine.Object has it:
    https://docs.unity3d.com/ScriptReference/Object-name.html
     
    Joe-Censored likes this.
  3. Calmax

    Calmax

    Joined:
    Aug 8, 2017
    Posts:
    20
    Thanks for the reply and apologies for the vague question. Here is an attachment screenshot of the error message. Hope this helps.
     

    Attached Files:

  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    6,542
    You've posted the same QuestGiver class twice, and this line looks problematic. Don't know if that is related to your issue though.

    Code (csharp):
    1. private Quest Quest { get; set; }
    Where's the "Interactable" base class? I'm guessing is has its own triggering and triggeringNpc fields based on the error messages.
     
    Last edited: Aug 21, 2019
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,702
    So your error says its the properties 'triggering' and 'triggerinNpc'.

    I don't know what you have in your 'Interactable' class, maybe it too has those properties?

    Also I noticed that those fields are private on NPC. Are you expecting them to not be serialized? You may want to attribute them as '[System.NonSerialized]'
     
  6. Calmax

    Calmax

    Joined:
    Aug 8, 2017
    Posts:
    20
    Here is the Intractable class:

    Code (CSharp):
    1. using UnityEngine.AI;
    2. using UnityEngine;
    3.  
    4. public class Interactable : MonoBehaviour
    5. {
    6.  
    7.     private GameObject triggeringNpc;
    8.     private bool triggering;
    9.  
    10.     public string[] dialogue;
    11.  
    12.     private void Start()
    13.     {
    14.        
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         if (triggering)
    21.         {
    22.  
    23.             if (Input.GetKeyDown(KeyCode.E))
    24.             {
    25.                 Interact();
    26.             }
    27.         }
    28.     }
    29.  
    30.     private void OnTriggerEnter(Collider other)
    31.     {
    32.         if (other.tag == "NPC")
    33.         {
    34.             triggering = true;
    35.             triggeringNpc = other.gameObject;
    36.         }
    37.     }
    38.  
    39.     private void OnTriggerExit(Collider other)
    40.     {
    41.         triggering = false;
    42.         triggeringNpc = null;
    43.     }
    44.  
    45.     public virtual void Interact()
    46.     {
    47.         Debug.Log("Interacting with base class.");
    48.     }
    49.  
    50. }
    51.  
    All I need is for the NPC to interact with the player, assign a quest and record when the quest is complete so they can receive a reward.

    Do I basically need to remove the duplicates of the "triggering" and "triggeringNPC"?
     
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,702
    Yes, you basically need to remove those duplicates. It's usually a bad idea to have fields of the same name in a child class as that of a parent class.
     
    Joe-Censored and Calmax like this.