Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by wlrnd · Oct 02, 2016 at 05:51 PM · playertextboxout of range

IndexOutOfRangeException: Array index is out of range.

I'm practicing activating and deactivating dialog boxes and I'm following this video tutorial : https://www.youtube.com/watch?v=7KNQYPcx-uU

but somehow i cannot get the dialog to pop up and i feel like it is because of this error??

This is the error:

IndexOutOfRangeException: Array index is out of range. TextBoxManager.Update () (at Assets/Scripts/TextBoxManager.cs:43)

This is the code with error:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class TextBoxManager : MonoBehaviour {
 
     public GameObject textBox;
 
     public Text theText;
 
     public TextAsset textfile;
     public string[] textlines;
 
     public int currentline;
     public int endAtLine;
 
     public playercontroller player;
     public bool isActive;
     public bool stopPlayerMovement;
     // Use this for initialization
     void Start () {
 
         player = FindObjectOfType<playercontroller> ();
 
         if(textfile != null)
         {
             textlines = (textfile.text.Split ('\n'));
 
 
 
         }
 
         if (endAtLine == 0) {
         
             endAtLine = textlines.Length - 1;
         
         }
             
         if (isActive) {
             EnableTextBox ();
         } 
         else {
             DisableTextBox ();
 
         
         }
 
     }
 
     void Update(){
     
         if (!isActive) {
             return;
 
         }
 
         theText.text = textlines [currentline];
     
         if (Input.GetKeyDown (KeyCode.Space)) {
         
             currentline += 1;
         
         }
 
         if (currentline > endAtLine) {
 
             DisableTextBox ();
         }
             
 
     }
 
     public void EnableTextBox(){
     
         textBox.SetActive (true);
         isActive = true;
 
         if (stopPlayerMovement) {
         
             player.canmove = false;
         
         
         }
 
     }
 
     public void DisableTextBox(){
     
         textBox.SetActive (false);
         isActive = false;
 
         player.canmove = true;
 
     }
 
     public void ReloadScript(TextAsset theText)
     {
         if (theText != null) {
         
             textlines = new string[1];
             textlines = (theText.text.Split ('\n'));
         
         }
     }
 
 
 }
 

and this is the other code that I'm using: using UnityEngine; using System.Collections;

 public class ActivateTextAtLine : MonoBehaviour {
 
     public TextAsset theText;
 
     public int startline;
     public int endline;
 
     public TextBoxManager theTextBox;
 
     public bool destroyWhenActivated;
 
     public bool requireButtonPress;
     private bool waitForPress;
 
     // Use this for initialization
     void Start () {
     
         theTextBox = FindObjectOfType<TextBoxManager> ();
     }
     
     // Update is called once per frame
     void Update () {
     
         if (waitForPress && Input.GetKeyDown (KeyCode.K)) {
         
             theTextBox.ReloadScript (theText);
             theTextBox.currentline = startline;
             theTextBox.endAtLine = endline;
             theTextBox.EnableTextBox ();
 
             if(destroyWhenActivated)
             {
                 Destroy(gameObject);
             }
         }
     }
 
     void OnTriggerEnter2d(Collider2D other){
 
         if (other.name == "Player") {
         
             if (requireButtonPress) {
             
                 waitForPress = true;
                 return;
             }
             theTextBox.ReloadScript (theText);
             theTextBox.currentline = startline;
             theTextBox.endAtLine = endline;
             theTextBox.EnableTextBox ();
 
             if(destroyWhenActivated)
             {
                 Destroy(gameObject);
             }
         
         }
     
     }
 
     void onTriggerExit2d(Collider2D other)
     {
         if (other.name == "Player") {
         
             waitForPress = false;
         }
     }
 
 }
 

thank you

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by TBruce · Oct 02, 2016 at 07:27 PM

TextBoxManager looks fine. I added some checks to the file which will log errors instead of a crash to notify you where the problem occurs

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class TextBoxManager : MonoBehaviour
 {
     public GameObject textBox;
 
     public Text theText;
 
     public TextAsset textfile;
     public string[] textlines;
 
     public int currentline;
     public int endAtLine;
 
     public playercontroller player;
     public bool isActive;
     public bool stopPlayerMovement;
 
     // Use this for initialization
     void Start ()
     {
         player = FindObjectOfType<playercontroller> ();
 
         if(textfile != null)
         {
             textlines = (textfile.text.Split ('\n'));
         }
 
         if (endAtLine == 0)
         {
             endAtLine = textlines.Length - 1;
         }
 
         if (isActive)
         {
             if (textbox == null)
             {
                 Debug.LogError("TextBoxManager.Start.EnableTextBox: textbox is null. Please replace and try again.");
             }
             EnableTextBox ();
         } 
         else
         {
             if (textbox == null)
             {
                 Debug.LogError("TextBoxManager.Start.DisableTextBox: textbox is null. Please replace and try again.");
             }
             DisableTextBox ();
         }
     }
 
     void Update()
     {
         if (!isActive)
         {
             return;
         }
 
         theText.text = textlines [currentline];
 
         if (Input.GetKeyDown (KeyCode.Space))
         {
             currentline += 1;
         }
 
         if (currentline > endAtLine)
         {
             if (textbox == null)
             {
                 Debug.LogError("TextBoxManager.Update.DisableTextBox: textbox is null. Please replace and try again.");
                 return;
             }
             DisableTextBox ();
         }
     }
 
     public void EnableTextBox()
     {
         textBox.SetActive (true);
         isActive = true;
 
         if (stopPlayerMovement)
         {
             player.canmove = false;
         }
     }
 
     public void DisableTextBox()
     {
         textBox.SetActive (false);
         isActive = false;
         player.canmove = true;
     }
 
     public void ReloadScript(TextAsset theText)
     {
         if (theText != null)
         {
             textlines = new string[1];
             textlines = (theText.text.Split ('\n'));
         }
     }
 }

However in ActivateTextAtLine this line could be a problem if you have destroyWhenActivated.

 Destroy(gameObject);

Is ActivateTextAtLine attached to the same game object the same game object as the TextBoxManager.textBox object? It is difficult to tell.

Also, do you have destroyWhenActivated set to true?

Comment
Add comment · Show 1 · Share
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
avatar image TBruce · Oct 03, 2016 at 07:06 PM 1
Share

@wlrnd Could you please be so kind as to click the "Accept" button above to accept the answer if you have found this helpful?

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

77 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

Related Questions

What this error? 0 Answers

The shooting mechanics 0 Answers

Unty 3D C# Load other scene when bool = true, otherwise perform teleportation as commanded. 1 Answer

NavMesh always makes player move at origin on startup 1 Answer

Can someone please tell me whats wrong with this?, because when i put it says error in the GetAxis 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