Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ishmaell · Jun 15, 2020 at 09:34 AM · unity 2ddialoguedialog

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

asd.png (27.8 kB)
capture.png (6.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

133 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to Move player by itself to a positions when talking to an NPC? 0 Answers

Cant Get Dialogue Trigger Working 0 Answers

Conversation Bubbles and starting it 1 Answer

How can i change Scene when my dialogue is end 1 Answer

How to save and load the state of dialogue when changing scenes 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges