Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 cipherr · May 23, 2013 at 11:13 PM · c#increment

Incrementing a string array (c#)

Hello,

I'm working on what will be a universal script for talking to NPCs in my game. The length of the string will be determined in the inspector, as well as any text that will be said will be entered there as well. So what I'm trying to do is make it so that when you press a key (E), it increases the array by 1 (thus changing the text on screen), and when the array reaches its end, I will have it close the text box (that part isn't in yet). So my question is this: how do I increase the array by one? I've looked everywhere for the past hour and have had no luck, even though the answer sounds simple at least. Here's the (relevant) code I'm using:

 void OnGUI () {
         if(talking == true){
             GUI.Box(new Rect(30,30,650,120), "");
             GUI.Box(new Rect(40,40,100,100), "faceplate");
             GUI.Label (new Rect (150,40,650,120), mainText[0]);
             if(Input.GetKeyDown(KeyCode.E)){
                  for(int i = 0; i <= mainText.Length-1; i++){
                     //make mainText[] go to the next element in its index
                     }
             }
         }
     }
 

I think the answer is very simple, but I really couldn't find anything. Any help is appreciated, thanks

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

2 Replies

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

Answer by robertbu · May 23, 2013 at 11:17 PM

First you the Input.GetKeyDown() needs to be in Update(). OnGUI() often gets called multiple times per frame, so you code will advance multiple times. Then you need a variable to keep track of the current message. The restructured code might look like this (untested):

 private int iCurr = 0;
 
 Update() {
          if(Input.GetKeyDown(KeyCode.E)){
              iCurr++;
              if (iCurr >= mainText.Length)
                  talking = false;
          }
 }
 
 void OnGUI () {
        if(talking){
          GUI.Box(new Rect(30,30,650,120), "");
          GUI.Box(new Rect(40,40,100,100), "faceplate");
          GUI.Label (new Rect (150,40,650,120), mainText[iCurr]);
        }
     

}

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 cipherr · May 24, 2013 at 01:04 AM 0
Share

Ah, I see what you mean. I guess I was just trying to approach it a weird angle. This works pretty efficiently when adapted to what I already had, thanks!

avatar image
0

Answer by darthbator · May 23, 2013 at 11:18 PM

You're looking to shift "forward" through the index correct? Not dynamically re size the array? The way I do this is by making a "key" to the array and incramenting over it. In your block here you would want.

mainText[i] or mainText[i + 1] depending on where you want to be in the array in the loop.

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 cipherr · May 24, 2013 at 01:06 AM 0
Share

Yeah I had been trying putting variations of that (mainText[i]) in there, but I was unable to figure out the proper syntax to have it not spit an error back at me. Robertbu has a good (but similar) alternative.

Thanks for helping

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

14 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Need help fixing this script again... Sorry guys. 0 Answers

Flip over an object (smooth transition) 3 Answers

Problem with UI slider to make it increment other variables 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