Weasel Bandit
Weasel Bandit

Reputation: 11

Unity: Calling a method from another script

I am completely new to Unity and all other answers I've found for this go over my head.

So far I've run everything from the same script, which is getting very big and messy. Therefore I am trying to learn how to call methods from other scripts.

I have a dropdown menu with the code in one script and I'm trying to call that code from another.

ScriptA:

using UnityEngine;

public class ChoseLanguage: MonoBehaviour
{
    public TMPro.TMP_Dropdown myDrop;

    DisplayController displayController;

    public void DropdownChooseLanguage()
    {
        if (myDrop.value == 1)
            PlayerPrefs.SetString("chosenLanguage", "Spanish");

        if (myDrop.value == 2)
            PlayerPrefs.SetString("chosenLanguage", "Japanese");

        if (myDrop.value == 3)
            PlayerPrefs.SetString("chosenLanguage", "Korean");

        if (myDrop.value == 4)
            PlayerPrefs.SetString("chosenLanguage", "Icelandic");


        Debug.Log(PlayerPrefs.GetString("chosenLanguage"));

        displayController.DropdownSetLanguage();

    }
}

The selection code works by itself, and the debug.Log shows that the chosen language is being saved correctly to PlayerPrefs.

The error comes when it tries to read the "displayController.DropdownChooseLanguage();" line. (Line 28)

Unity gives this error:

NullReferenceException: Object reference not set to an instance of an object
ChoseLanguage.DropdownChooseLanguage () (at Assets/Scripts/ChoseLanguage.cs:28)

Script B

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;
using TMPro;

public class DisplayController : MonoBehaviour
{

...

    public void DropdownSetLanguage()
    {
        SetFileName();
        setLanguage.gameObject.SetActive(false);
        Start();
    }

...
}

Earlier, the exact same code from Script A was placed in ScriptB and all the code worked as it should.

This is a very noob question but I have simply just never been able to understand how exactly to access other scripts correctly.

Any help will be greatly appreciated.

Thanks.

EDIT: I found a solution to this but I'll keep the question up in case other beginners have the same problem or if anyone has a better solution.

I made the DisplayController displayController; into public DisplayController displayController; and then dragged the gameobject with the displaycontroller script attached into the slot for it.

Upvotes: 1

Views: 1628

Answers (1)

Mustafa İPEK
Mustafa İPEK

Reputation: 51

DropdownSetLanguage is public and it seems you think you can access it by creating a DisplayController object named displayController.

It is ok about to access a class but, displayController should be null in this state. Because you have to assign an initial DisplayController to displayController in your first code.

You can add public or [Serializable] tag in front of DisplayController displayController; in your first code then, you can add a GameObject which has a DisplayController script in it in Unity editor.

Upvotes: 1

Related Questions