New to Unity
New to coding
So I’m trying to code a dialogue system for a game I’m working on and while I have the text itself functioning, I’m trying to implement functionality for displaying portraits of characters who’re talking, highlighting whoever is speaking and changing expressions of the character based on the line being spoken.
I had all the dialogue saved to a scriptable object and was going to create an array of GameObjects within the scriptable object but when I did it wouldn’t let me insert GameObjects from my scene. I figured it was because a scriptable object can’t be active as a component in the scene itself so it can’t reference objects in a scene. I had made a script to reference the scriptable object that I could attach to any interactable NPC so my other scripts could access data from it, so I figured I would create my array inside the script (NPCDialogueData.cs) and set the size of the array based off of a variable I would set inside my scriptable object. Unfortunately when I try to reference the array size variable (currentSpeakerQuantity) from my scriptable object to my NPCDialogueData script, it gives me the error on line 7, A field initializer cannot reference the non-static field, method, or property ‘NPCDialogueData.dialogueData’
public class NPCDialogueData : MonoBehaviour
{
private int currentSpeakerQuantity = dialogueData.CurrentSpeakerQuantity;
public DialogueObject dialogueData;
public GameObject[ ] currentSpeaker = new GameObject[currentSpeakerQuantity];
}
There’s the code for the script.
[CreateAssetMenu(menuName = "Dialogue/Dialogue Object")]
public class DialogueObject : ScriptableObject
{
[SerializeField] [TextArea] private string[ ] dialogue;
[SerializeField] private int currentSpeakerQuantity;
[SerializeField] private Sprite[ ] spriteInfo;
public string[ ] Dialogue => dialogue;
public int CurrentSpeakerQuantity => currentSpeakerQuantity;
public Sprite[ ] SpriteInfo => spriteInfo;
}
And there’s the code for my scriptable object
I looked into this error message and saw it was because the field I was referencing was non-static i.e. I was initializing an instance member using an instance member so the order of initialization has the possibility of throwing an error. The solution to this is to set the dialogueData variable in the NPCDialogueData script to static, but doing so causes another issue in my code. In my player controller script, I have it so that when the player walks into a collider with the “Interactable” tag, it sets the variable currentInteractableDialogueData to the variable dialogueData found in my NPCDialogueData script up above. Making dialogueData static throws an error here.
protected void OnTriggerEnter(Collider other)
{
if(other.tag == "OWEnemy")
{
Debug.Log("Start Combat Transition");
enemyCollide.Invoke();
}
else if(other.tag == "Interactable")
{
Debug.Log("Show Button Prompt");
ShowButtonPromptA();
currentInteractable = other.gameObject;
currentInteractableDialogueData = currentInteractable.GetComponent<NPCDialogueData>().dialogueData;
}
}
There’s the relevant snippet of code from my PlayerController script. I don’t want to directly reference my scriptable object anywhere in these scripts in order to allow me to make multiple scriptable objects for different NPC’s, but I’m not sure if this is going to be possible, or whether or not I’ll need to refactor my dialogue system. I looked pretty hard online and I couldn’t find anything that applied as far as I could tell. I apologize for my (probably) spaghetti code as I have only been coding on and off (mostly off) for a little less than a year. I am also generally new to Unity as well and have been working with it for about the same timeframe. If any more info/code is needed, I would be happy to provide it and thank you for the help!