- Home /
array knowledge is cloudy at best
i'm working on making a tutorial, and i want it so when you press the next button, it goes to the "next page" and the back button will go to the "last(previous) page" that, i can do; but how would i call something from an array?
like:
var message : String[];
function Update() { messageValue++; messageValue--; }
how would you do that?
//i use the "for(i = 0; i<Array.Length; i++)"
but i don't know what that does exactly, and i dont know how to use [i]... could someone explain to me how i would do that with an array?
thanks in advance!
Edit:
the script in the below answer looks okay, but this error comes up:
NullReferenceException: Object reference not set to an instance of an object
Tutorial.GoUpPage () (at Assets/Assets/'Scripts'/Faux Move/GUI/Tutorial.js:22)
Tutorial.Update () (at Assets/Assets/'Scripts'/Faux Move/GUI/Tutorial.js:13)
and it comes up in the
function GoUpPage()
{
if(currentPage < pages.length)// RIGHT HERE.... but
{
currentPage += 1;
print(pages[currentPage]);
}
}
and i notice the other reference here:
function Update()
{
if(Input.GetKeyDown("w"))
{
GoUpPage(); // RIGHT HERE
}
}
Read my comment below. Use the script i edited add in ur key down code. should work golden. (=
Answer by s4vi0r · Dec 29, 2010 at 08:36 PM
You might try something like:
var pages : Array; var currentPage : int;
function Start()
{
//pages would be what ever type of data ur pages are...perhaps they are images. in my example they are going to be strings.
pages = new Array("info on page 1", "info on page 2", "info on page 3");
//this will print the first page info
currentPage = 0;
print(pages[currentPage]);
}
function Update()
{
///write some sort of onkeydown stuff here to call your functions
}
function GoUpPage()
{
if(currentPage < pages.length)
{
currentPage += 1;
print(pages[currentPage]);
}
}
function GoDownPage()
{
if(currentPage > 0)
{
currentPage -= 1;
print(pages[currentPage]);
}
}
function PrintAllPages()
{ for(i = 0; i < pages.length; i++) { print(pages[i]); } }
that looks good, but im gonna try it now, and i think i see what you're doing, so in the functions (besides PrintAllPages), the output is pages[currentPage]? because if that's the case, i learned something. if not, then... i'll just use this example and build from it.
by the way, you have Array(); and it should be Array[]; but, you'd notice that in the console error thing, so that's nothing, but there's a null reference exception co$$anonymous$$g up at this line: "if(currentPage < pages.length)" in the GoUpPage function.
There are two types of arrays in Unity, builtin arrays and normal Javascript Arrays.
Builtin arrays (native .NET arrays), are extremely fast and efficient but they can not be resized.
I used a normal JavaScript array. Because it has functions that allow you to resize sort and add elments..etc.
The only error the first line. it should be:
var pages : Array;
check here for more info on differences in the 2 arrays:
http://unity3d.com/support/documentation/ScriptReference/Array.html
right, because the [] specify the .NET arrays (right?) and you only put those behind the class ie: var food : Food[]; and you could add chicken and s$$anonymous$$k (sorry, that was the first thing that ca,e to $$anonymous$$d... but that works, a real exapmple:) var word : String[]; but anyway, your script works, i think it might work with my script, so thanks!
Your answer