Text, Dictionary, and GetComponent
In the QuestGiving script, there's a dictionary. The script checks to see if a player is close enough and then plays an audio track. Once the audio track is finished, a new quest that contains the quest name and a short description is added to the dictionary.
In the QuestsText script, a Text UI looks for any object that has the tag "Quest Giver". There are two of them in my game, Froggy and Opossum. The dictionary from the first script is then constantly being updated and should display the quest names, or keys, to the Text UI. The thing is, only one of them is stored and displayed, so I have to use the FindGameObjectsWithTag, which can contain multiple GameObjects. However, I can't use the Get Component code to work with the array because: "GameObject[]' does not contain a definition for 'GetComponent' and no accessible extension method 'GetComponents' accepting a first argument of type 'GameObject[]' could be found (are you missing a using directive or an assembly reference?)"
So my question is, how can I get the dictionary keyS to be able to be displayed to the Text object?
Here's my two scripts being used:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class QuestGiving : MonoBehaviour
{
public GameObject player;
public GameObject questGiver;
public new AudioSource audio;
private int preventLoop = 0;
// The dictionary that will conntain all quests, a Text UI element is has an Update function to the keys of this
public Dictionary<string, string> allquests = new Dictionary<string, string> { };
void Update()
{
// Gets distance between player and quest giver
float dist = Vector2.Distance(player.transform.position, questGiver.transform.position);
if (dist <= 0.5 && preventLoop < 1)
{
if (Input.GetKeyUp("e") || Input.GetButton("Controller Interact"))
{
audio.Play();
// Gets the audio length and invokes the AudioFinished function after the audio has started and the length has passed
Invoke("AudioFinsihed", audio.clip.length);
preventLoop++;
}
}
foreach (string key in allquests.Keys)
{
Debug.Log(key);
}
}
void AudioFinsihed()
{
// If statements that get the key from the dictionary and adds the key to the Text element
if (gameObject.name == "Froggy")
{
allquests.Add("Washington was bomb", "not literally");
}
if (gameObject.name == "Opossum")
{
allquests.Add("5 Seconds", "it's actually 4");
}
}
}
and
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class QuestsText : MonoBehaviour {
public Text questsText;
public int number = 2;
QuestGiving Qd;
public GameObject[] quests = new GameObject[2]; // Goes with second choice
public void Start()
{
// First Way, only shows 1 quests, the questGiver seems to be picked randomly as it has switched between Opossum and Froggy
GameObject QD = GameObject.FindGameObjectWithTag("Quest Giver");
Qd = QD.GetComponent<QuestGiving>();
// Second way - the GetComponents doesn't seem to work though, thus I can't as easily place it into the Text
// quests = GameObject.FindGameObjectsWithTag("Quest Giver");
// Qd = quests.GetComponent<QuestGiving>();
}
private void Update()
{
// Goes with second choice
foreach (GameObject questGiver in quests)
{
Debug.Log(questGiver);
}
foreach (string key in Qd.allquests.Keys)
{
Debug.Log(key);
questsText.text = "Quests: \n - " + key;
}
}
}
Answer by Ravvenn · Nov 12, 2018 at 03:44 AM
Hi, just checking back in to give my solution.
Honestly, the easiest solution to this was to put the dictionary in its own script which was on an empty GameObject. That way, these two scripts had to use the FindGameObjectWithTag and it would only find one object. The dictionary could then be easily accessed from there.