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 castor · Dec 03, 2012 at 04:39 PM · arraysearchlength

Getting length of array based on a loop search?

I made a custom class called DialogBallon that contains a bunch of variables:

 class DialogBalloon {
     var id : int;
     var nextId = new Array();        //possible answers
     var actor : Transform;
     var listener : Transform;
     var title : String;
     var text : String;
     var waitingText : String;
     var isPlayerQuestion : boolean;        //If its a Question that the player can ask(needs to be unlocked to show)
     var isActorQuestion : boolean;        //If its a question that a Npc can ask.
     var isLastLine : boolean;              //Last line of a dialog.
     var isUnlocked : boolean;  //once unlocked the player can use it as option.
     var wasDisplayed : boolean;    //if it has already been said    
 }

and now i have several of these inside an Array:

 var allDialogs = new Array();    //Array that contains all the game dialogs
 
 //adding the dialogs
 
 allDialogs.Add(exitDialogLine);
 allDialogs.Add(politeEndLine);
 allDialogs.Add(beingIgnored);
 etc...

During my game I do a filter of the ones of a certain type to generate the buttons that allow the player to use those dialogs and it works well:

         for(var possibleQuestion : DialogBalloon in dialogManager.allDialogs){    
             if(
             possibleQuestion.isPlayerQuestion &&
             possibleQuestion.isUnlocked && 
             possibleQuestion.wasDisplayed == false
             ){        
                 var width : Vector2 = GUI.skin.GetStyle("keywords").CalcSize(GUIContent(possibleQuestion.title));
                     
                 if( GUI.Button (Rect (0, yOffset, width.x, 30), GUIContent (possibleQuestion.title))){
                 //do stuff
                 }
                 yOffset += 25;
                 totalYOffset = yOffset;
 
             }
         }

Using this for example will show 3 buttons for the 3 only questions out of around 30 that fit the if() requirements but I would also like a variable that tells me the total. If I do a dialogManager.allDialogs.Length inside the 'for' loop I always get the total and I can't figure out what I should write to get the correct results.

Any help very appreciated!

UPDATE - RESOLVED.

The proposed solution didn't quite work (it would just increase forever) but somehow I got to work by adding a second variable (possibleQuestionTotal) outside the function on the top of the script. Maybe because its a function? Would love to understand why it had to be this way.

 function QuestionsGUI(){
     var yOffset  = 0;
     var AreaWidth = 500;
     var possibleQuestionSize = 0;
     
     GUILayout.BeginArea(Rect ((Screen.width * 0.6) - (AreaWidth * 0.5),(Screen.height * 0.83), AreaWidth,500));
     GUILayout.BeginVertical();    
         for(var possibleQuestion : DialogBalloon in dialogManager.allDialogs){    
             if(
             possibleQuestion.isPlayerQuestion &&
             possibleQuestion.isUnlocked && 
             possibleQuestion.wasDisplayed == false
             ){                    
                 var width : Vector2 = GUI.skin.GetStyle("keywords").CalcSize(GUIContent(possibleQuestion.title));
                     
                 if( GUI.Button (Rect (0, yOffset, width.x, 30), GUIContent (possibleQuestion.title))){
                     dialogManager.currentDialogDisplay = possibleQuestion;
                     possibleQuestion.wasDisplayed = true;
                     showDialogLine = true;
                     showQuestions = false;
                     showAnswers = false;
                     showWaitingLine = false;
                     waitingTimer = 0.0;
                     
                     ResetSayDialogLine ();
                 }
                 yOffset += 25;
                 totalYOffset = yOffset;
                 possibleQuestionSize ++;
                 possibleQuestionTotal = possibleQuestionSize;
 
             }
         }
     GUILayout.EndVertical();
     GUILayout.EndArea();
 }



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
1
Best Answer

Answer by Landern · Dec 03, 2012 at 05:23 PM

Create a temporary var, add to it as you loop through.

        var countTemp:int = 0;
 
        for(var possibleQuestion : DialogBalloon in dialogManager.allDialogs){    
          if(
          possibleQuestion.isPlayerQuestion &&
          possibleQuestion.isUnlocked && 
          possibleQuestion.wasDisplayed == false
          ){
           countTemp++;     
           var width : Vector2 = GUI.skin.GetStyle("keywords").CalcSize(GUIContent(possibleQuestion.title));
 
           if( GUI.Button (Rect (0, yOffset, width.x, 30), GUIContent (possibleQuestion.title))){
           //do stuff
           }
           yOffset += 25;
           totalYOffset = yOffset;
 
          }
        }
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 castor · Dec 03, 2012 at 10:29 PM 0
Share

Thanks. I tried it but it didnt work, the variable would just keep adding over and over. I realized that for it to work I needed to loop it inside the function and then have the final result outside the script. Adding the result to an update on the question.

If you somehow understand why I had to do it this way I would love to learn it.

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

10 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

Related Questions

Search an array? 2 Answers

array out of range while updating mesh variables 4 Answers

array.length read only? workaround? 2 Answers

Checking array fails each time. But change anything and resave and it works until unity closes...... 1 Answer

Accessing List Array in Other Script Problem 2 Answers


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