Max
Max

Reputation: 53

Calling an Object from another Script

This might be a super simple question, but for some reason I can't get it to work:

I have two scripts, both attached to the same GameObject.

One script has a dictionary:

public class RPG_Implementierung : MonoBehaviour
{
    public Dictionary<string, string> StoryText = new Dictionary<string, string>();
    
    void Start()
    {
        StoryText.Add("1", "This is the first Entry");
    }

}

The other script wants to call that Dictionary. The method SendMessageToChat` is defined in this script and works well as long as it's not referencing the other script.

The first thing I tried didn't work, I get the Error: CS0120 An object reference is required for the non-static field, method, or property

public class GameManager : MonoBehaviour
    {
    void Update()
            {
                
                    if (Input.GetKeyDown(KeyCode.Y))
                    {
                        SendMessageToChat(RPG_Implementierung.StoryText["1"]);
                        
                    }
                }
    }

I

this also doesn't work, it gives me the Error CS0119 'RPG_Implementierung' is a type, which is not valid in the given context

public class GameManager : MonoBehaviour
{
void Update()
        {
            
                if (Input.GetKeyDown(KeyCode.Y))
                {
                    SendMessageToChat(GetComponent(RPG_Implementierung).StoryText["1"]);
                   
                }
            }
}

Can someone please tell me what I did wrong? In standard C# all I would have to do is to set the other class to public and then I can reference it and access it's objects, why doesn't this work in Unity?

Upvotes: 1

Views: 891

Answers (1)

TEEBQNE
TEEBQNE

Reputation: 6266

To reference another component on a GameObject, you will need to grab that reference either by serializing the field in the inspector (Making it public or using the attribute [SerializeField].

I am not sure how many places you want to eventually call the method you are trying to invoke, but if it is from a bunch of different places, you might want to consider the Singleton pattern.

To quickly fix your current issue, on your GameManager.cs, do one of these two things:

public class GameManager : MonoBehaviour
{
     [SerializeField] private RPG_Implementierung rpgImplement = null;
// OR
     public RPG_Implementierung rpgImplement;

     void Update()
     {
         if (Input.GetKeyDown(KeyCode.Y))
         {
            SendMessageToChat(rpgImplement.StoryText["1"]);   
         }
     }
}

Edit: If you want to use the GetComponent in the Update here is how you would call it. I would advise against this as calling a GetComponent in an Update can be quite costly for performance if called frequently. It is better to store the reference to later use.

public class GameManager : MonoBehaviour
{
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.Y))
         {
            SendMessageToChat(GetComponent<RPG_Implementierung>().StoryText["1"]);   
         }
      }
}

Upvotes: 1

Related Questions