How to limit text lines being shown

Im using this script to display a list of items to be found in my scene once found the items are hidden.

function Update()
{
  var txt: String = "   Items to find:
";

  if (item1found==false) txt += "Mirrior
"; // add the items that were not found yet
  if (item2found==false) txt += "Hotwater Bottle
";
  if (item3found==false) txt += "Gold plate
";
  if (item4found==false) txt += "Pair of Oakley's
";
  if (item5found==false) txt += "Coffee Mug
";
  if (item6found==false) txt += "Ipad 
";
  if (item7found==false) txt += "winebottle
"; 
  if (item8found==false) txt += "
";
  if (item9found==false) txt += "
";
  if (item10found==false) txt += "
";
  if (item11found==false) txt += "
";
  if (item12found==false) txt += "
";
  gText.text = txt;
}

I was wonder if there is a way to limit the amount to show like 6 items at a time as there will be about 25-30 items and that would cover the screen. Any and all suggestions are welcome.

You could create a “pseudo scroll” function: count the lines to be added, but effectively add only those that are inside the “viewing” area. You can use two keys to scroll the window, like this:

var maxLines: int = 6; // size in lines
var top: int = 0; // current top line
private var nLine: int;
private var txt: String;

function AddItem(name: String){
  nLine++; // count the line anyway
  // but add it only if inside window
  if (nLine > top && nLine <= top + maxLines){
    txt += nLine + ":" + name + "
"; 
  }
}

function Update(){
  txt = "   Items to find:
";
  nLine = 0; // init line number
  if (item1found==false) AddItem("Mirror");
  if (item2found==false) AddItem("Hotwater Bottle");
  if (item3found==false) AddItem("Gold plate");
  if (item4found==false) AddItem("Pair of Oakley's");
  if (item5found==false) AddItem("Coffee Mug");
  if (item6found==false) AddItem("Ipad");
  if (item7found==false) AddItem("Wine Bottle"); 
  gText.text = txt;
  // scroll up or down with keys PgUp/PgDown:
  if (Input.GetKeyDown("page up") && top > 0) top--;
  if (Input.GetKeyDown("page down") && nLine > top + maxLines) top++;
}

NOTE: I added the line number to improve the user interface; if you don’t want it, change the last line to:

  txt = name + "

";

var count : int = 0;

if (!item1found && count < 6)
{
  count++;
  txt += "Mirrior

";
}

etc.

Or if you don't want to read any documentation at all...

Here's a script that demonstrates how to use a ScrollView.

It also demonstrates how to make a list of buttons: one for each item in an array.

When you click on a button, the item is removed from the list (from your other question).

If you look at the definition of a SearchObject, you'll see that it contains a Transform.

This would allow you do drag and drop transforms onto the Inspector and reference them from your code (if you want).

Hope this helps!

/* an array of SearchObjects */

var searchItemList     :  SearchObject []; 

var scrollRect         :  Rect     =  Rect(200, 200, 220, 220);
var itemRect           :  Rect     =  Rect(0, 0, 200, 600);
var buttonRect         :  Rect     =  Rect(0, 0, 200, 100);
var scrollPos          :  Vector2  =  Vector2.zero;

/*  fill the array with random stuff for testing */

function Start ()
{
    searchItemList = new SearchObject [6];

    searchItemList[0] = new SearchObject("Grenade");    
    searchItemList[1] = new SearchObject("Bunny Slippers");
    searchItemList[2] = new SearchObject("Flame Thrower");
    searchItemList[3] = new SearchObject("Lollypop");
    searchItemList[4] = new SearchObject("Junk Pile");
    searchItemList[5] = new SearchObject("Car");
}

/*  draw a GUI to show what has NOT been found */

function OnGUI ()
{
    scrollPos = GUI.BeginScrollView(scrollRect, scrollPos, itemRect);   

    var theRect : Rect = buttonRect;

    /* loop through each SearchObject in the array */

    for ( var thisItem : SearchObject in searchItemList ) {

        /* skip items that have been found */

        if ( thisItem.isFound ) {
            continue;
        }

        /* draw a button for each SearchObject in the array */

        if ( GUI.Button(theRect, thisItem.name) ) {
            thisItem.isFound = true;
        }

        /* advance the rectangle for the next button */

        theRect.y += theRect.height;
    }

    GUI.EndScrollView();
}

/*  a definition for a simple example class named SearchObject.    */

/*  note that a SearchObject has a transform associated with it.   */
/*  you could assign something to that by dragging an object onto  */
/*  the appropriate slot in the inspector                          */

class SearchObject
{
    var name        :  String;
    var isFound     :  boolean;
    var transform   :  Transform;

    function SearchObject ( theName : String )
    {
        name    =  theName;
        isFound =  false;
    }
}

Unity has great documentation. All you need to do is read it.

You need to use the function named BeginScrollView.

You can read all about it here.

Basically, you declare one rectangle to hold the scroll box and another to hold the contents. The return value of the function is a vector that represents the scroll position.

You pass the scroll position in as a parameter to the function (it’s called scrollPosition in the documentation) and then get it back as the return value to keep track of where the slider bar is.

You could probably try searching for BeginScrollView and get a lot of information.