I have been trying to find a solution for the last couple of hours so please hear me out. At first I got a NullReferenceException, which I resolved(?) by initializing the class first (This is my problem! I can't seem to initialize my class sucessfully). However, here are my next errors. I tried using the new
keyword, which resulted in You are trying to create a MonoBehaviour using the 'new' keyword
. Next I tried GetComponent<>()
, which resulted in a CS0120
, that I didn't know how to fix. The AddComponent<>()
, resulted in a CS0103
The name AddComponent does not exist in the current context
. What I tried next was GameObject.AddComponent<>()
, which threw a CS0120
, and gameObject.Addcomponent<>()
which threw a CS0236
.
Here is my code for the class I am trying to initialize if it should be needed:
public class Dialogue : MonoBehaviour {
public TextMeshProUGUI textDisplay;
public string[] sentences;
private int index;
public float typingSpeed;
public GameObject continueButton;
void Update() {
if (textDisplay.text == sentences[index]) {
continueButton.SetActive(true);
}
}
public IEnumerator Type() {
foreach (char letter in sentences[index].ToCharArray()) {
textDisplay.text += letter;
yield return new WaitForSeconds(0.01f);
}
}
public void NextSentence() {
continueButton.SetActive(false);
if (index < sentences.Length - 1) {
index++;
textDisplay.text = "";
StartCoroutine(Type());
} else {
textDisplay.text = "";
continueButton.SetActive(false);
}
}
}
Here is from where I tried to initialize:
public class Mace : MonoBehaviour {
//Dialogue dialogue = gameObject.AddComponent<Dialogue>();
private void OnTriggerEnter2D(Collider2D otherCollider) {
if (otherCollider.CompareTag("Player")) {
StartCoroutine(dialogue.Type());
}
}
}