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 Jesus_Freak · Dec 29, 2010 at 08:15 PM · array

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
 }
}
Comment
Add comment · Show 1
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 s4vi0r · Dec 29, 2010 at 09:31 PM 0
Share

Read my comment below. Use the script i edited add in ur key down code. should work golden. (=

1 Reply

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

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 &lt; pages.length)
   {
     currentPage += 1;
     print(pages[currentPage]);
   }
 }

 function GoDownPage()
 {
   if(currentPage &gt; 0)
   {
     currentPage -= 1;
     print(pages[currentPage]);
   }
 }
 function PrintAllPages()

{ for(i = 0; i < pages.length; i++) { print(pages[i]); } }

Comment
Add comment · Show 5 · 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 Jesus_Freak · Dec 29, 2010 at 09:01 PM 0
Share

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.

avatar image Jesus_Freak · Dec 29, 2010 at 09:04 PM 0
Share

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.

avatar image s4vi0r · Dec 29, 2010 at 09:28 PM 0
Share

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

avatar image Jesus_Freak · Dec 29, 2010 at 09:33 PM 0
Share

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!

avatar image s4vi0r · Dec 29, 2010 at 09:40 PM 0
Share

np. glad I could help

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

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

random spawn locations.... 2 Answers

Instantiate into array : Out of range? 1 Answer

array.length read only? workaround? 2 Answers

Can i use objects from a builtin Array to populate a Js Array 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