- Home /
Script is stuck on one gameObject and wont work on multiple gameObjects
I wanted to make one script where I can modify the dialog of my Characters easily from the inspector. I made a canvas which has two images (dialog.png as a source) and each has its own text. ![alt text][1] For example the script shows this ![alt text][2] Meaning if the player is close to Character then dialog automatically runs. If Press E is selected then the user has to press the E button to run dialog. Both are done in a typewriter way. The automatic way works and runs perfectly but when I go to another gameObject with Press E active the script reruns the text for the first gameObject.
The code is as follows: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Dialog : MonoBehaviour { //Public public GameObject Character; public Image ParentImage; public string[] Sentences; public bool PressE; public MainCamera camera; public bool cameraEffect; //Private private GameObject Player; private Vector3 charPosition; //character position private string currentText = ""; private Vector3 PlayerPosition; private bool dialogDone; //done talking private bool wasThere; //was there so no need to repeat dialog private bool wantsToSkip; //User wants to skip dialog private Image dialogImage; private int pressed; // Start is called before the first frame update void Start() { dialogImage = ParentImage.GetComponent<Image>(); dialogImage.enabled = false; charPosition = Character.transform.position; Vector3 temp = gameObject.transform.position; temp.y = charPosition.y + 1; Player = GameObject.FindGameObjectWithTag("Player"); dialogDone = false; wasThere = false; wantsToSkip = false; pressed = 0; } // Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0)) //if left mouse clicked during dialog, skip it { Debug.Log("Mouse clicked"); wantsToSkip = true; } charPosition = Character.transform.position; PlayerPosition = Player.transform.position; float temp = charPosition.x - PlayerPosition.x; if (!PressE) if ((temp < 3 && temp > 2) && !wasThere) { if (cameraEffect) camera.CenterCameraOnObject(Character); StartCoroutine(TypeWriterEffect()); } else { if (Input.GetKeyDown(KeyCode.E) && pressed < 1) { Debug.Log("Pressed E and GameObject is: "+gameObject.name); StartCoroutine(TypeWriterEffect()); } } if (dialogDone) //if dialog is done { PressE = false; Player.GetComponent<Player>().canMove = true; wasThere = true; gameObject.GetComponent<Text>().text = ""; } } IEnumerator TypeWriterEffect() { Player.GetComponent<Player>().canMove = false; Player.GetComponent<Rigidbody2D>().velocity = Vector3.zero; for (int j = 0; j < Sentences.Length; j++) { float Delay = 0.5f; gameObject.GetComponent<Text>().color = Color.black; int i = 0; while (i < Sentences[j].Length + 1) { dialogImage.enabled = true; float delay = 0.1f; currentText = Sentences[j].Substring(0, i); int x = currentText.Length; if (x > 2 && !wantsToSkip) if (currentText[x - 1].Equals('.') && currentText[x - 2].Equals('.') && currentText[x - 3].Equals('.')) { Debug.Log("Pausing because there was a '..."); delay = 0.7f; } gameObject.GetComponent<Text>().text = currentText; wasThere = true; if (wantsToSkip) { i = Sentences[j].Length; Delay = 0.02f; } i++; yield return new WaitForSeconds(delay); } yield return new WaitForSeconds(Delay); wantsToSkip = false; } dialogImage.enabled = false; dialogDone = true; } }
[1]: /storage/temp/161771-capture.png [2]: /storage/temp/161773-asd.png