Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Ravvenn · Oct 29, 2018 at 11:35 PM · arraytextgetcomponentarraysdictionary

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;
         }
     }
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

175 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Getting an error when using an Array of String Arrays 0 Answers

condition for array of textures 1 Answer

Drag and Drop. Grid Array to save if something is placed not working. Fix?,Drag and Drop Grid array to save the positions of placed Object not working. Fix? 1 Answer

How to play a random audio clip from an array in C#? 3 Answers

Cannot access a array in another script 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges