1

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?

1 Answer 1

1

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"]);   
         }
      }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer! When I try any of your proposed solutions, there is no more error message in Visual Studio, but when I run the game and press the Y button, it doesn't work and I instead get an "NullReferenceException: Object Reference not set to an instance of an object" error in the Unity debug log.
@Max Yep! That would make sense. The null reference is is because the reference of your script is not yet assigned in the inspector. If you look at the Unity hierarchy with the gameobject that has both of these scripts selected, you should see the GameManager script now have a field named rpgImplement if you went with my first snippet. If you drag the gameobject into that empty slot, it will fill in the reference.
If you went with the second snippet and it is null, that would be because the object that the GameManager is not is not the same object as the RPG_Implementierung script. If that is the case, I would go with the first snippet and assign the reference in the inspector. Another alternative is finding the object by name, tag, type, etc. in a Start() function, but if you call it often I find it easier to assign references in the inspector.
Here's a more useful Unity doc to walk you through this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.