Accessing Variables in other Scripts

Hi there,

I try to set a bool variable in another script to true and cant get it working.

As i see it:

I did declare a public gameobject in ScriptA:

public GameObject  Mission1TriggerBox;

void OnTriggerEnter2D(Collider2D other)
{
if (Input.GetKeyDown("T")
{
Mission1TriggerBox._isEnabled = true; <---- ERROR
}
}

I moved the GameObject via Inspector into the public thingy in the Unity Editor…
then in ScriptB i wrote:

public bool _isEnabled;

But in ScriptA he wont find the _isEnabled… :confused:

The idea behind is that i stay in front of a Computer and when i press “T” then the Mission gets accepted, setting the Mission bool true so if i walk throught a door it teleports me to another area of the map…

Someone knows what im doing wrong there?

Look up in your favorite C# tutorial series the difference between static and instance variables.

This is a static reference, and the variable must therefore be declared static, and there is only ever ONE of them:

Mission1TriggerBox._isEnabled

I don’t think that’s what you want, because each mission would have its own _isEnabled field.

Over in script B, if that is ALSO a mono behavior, say “ScriptB” monobehavior, then your code would look like:

ScriptB otherScript = otherGameObject.GetComponent<ScriptB>();
otherScript.isEnabled = true;

otherGameObject could actually just be gameObject if both of these scripts are on the same gameObject together.

1 Like

So my problem is now that neither of both scripts find the other one just by writing it.
Do i have to set them somewhere so they know each other exists?

I dont quiet get what you trying to teach me…

ScriptB is laso MonoBehavior.
If i write "ScriptB otherScript = otherGameObject.GetComponent<>(); then it wont find the “GetComponent” in the drop down menu… and what is “otherGameObject”?

I think im missing something very basic but i cant figure out what :confused:

Please show the whole code, such that you can name things like you did.

1 Like
using UnityEngine;
using System.Collections;

public class Computer : MonoBehaviour {

    public Dialogue _dialoguescript;
    string[] DialogueStrings;

    public GameObject TextBox ;
    public GameObject HintergrundTextBox;

    public GameObject Mission1Trigger;

    public bool _FirstMissionEnabled = false;


    void Start()
    {

        TextBox.SetActive(false);
        HintergrundTextBox.SetActive(false);

    }

    void OnTriggerStay2D(Collider2D other){
        if (Input.GetKeyDown ("t"))
        {
            TextBox.SetActive(true);
            HintergrundTextBox.SetActive(true);

            DialogueStrings = new string[3];
            DialogueStrings[0] = "Hi there";
            DialogueStrings[1] = "Lets go to the first Mission!";
            DialogueStrings[2] = "Go to the Gate!";
            _dialoguescript.StartCoroutine(_dialoguescript.StartDialogue(DialogueStrings));

            Mission1Trigger._isEnabled = true; <<---- Here i want to set the bool to true, but it doesnt work..

          
        }
    }

    void OnTriggerExit2D(Collider2D other) {
        Debug.Log ("Und weg..");
        TextBox.SetActive(false);
        HintergrundTextBox.SetActive(false);
    }
}
using UnityEngine;
using System.Collections;

public class Mission1 : MonoBehaviour {

    public bool _isEnabled;

    void OnTriggerEnter2D(Collider2D other)
    {
        if (_isEnabled == true)
        {
          
            Debug.Log("YeeY");
         
        }
    }
}

Mission1Trigger is a game object. You declared that! A game object is not the same as a script. A game object can contain scripts and if you know the game object, you can get those components using GetComponent.
You could e.g. write

Mission1Trigger.GetComponent <Mission1> ()._isEnabled = true;

Like this you are changing _isEnabled for that component. The issue in your code is that game object doesn’t have _isEnabled.

In this context, it doesn’t make sense from my point of view to make Mission1Trigger a game object. You could directly specify it to be of type Mission1. Like that, no GetComponent is needed anymore.
If you change line 12 to:

public Mission1 Mission1Trigger;

you automatically solve the problem.

Ah, so there is my mistake.

So i can simple refer to external scripts by there scriptname, good to know :wink:

Thanks alot, all problem solved!