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…
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…
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
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
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: