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 logang · Apr 17, 2013 at 03:30 PM · c#guiarraytextures

holding GUITextures in a array? and changing the size from script?

I want to make a array holding GUItextures and I dont know how to come about this.. I Know java I think its start time I learn c# so I started off good and I am stuck here.. well here is the code:

 `using UnityEngine;
 using System.Collections;
 
 public class MainMenu : MonoBehaviour {
     
     public int[] TrackPictures = new int[2] {3, 4};
     public GUISkin myGUIskin;
     private int Track = 0;
     public int NumberOfTracks = 9;
     private int Number1More = 10;
     
     // Use this for initialization
     void Start () {
     TrackPictures = NumberOfTracks;
         
     
     //Makes sure that NO one make the "NumberOfTracks" go under "-"
         if(NumberOfTracks <= 0) {
             NumberOfTracks = 9;
         }
         
     //Make Sure that "Number1More" is set 1 more then the "NumberOfTracks"
     Number1More = NumberOfTracks;
     Number1More += 1;
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     
     void OnGUI () {
     
     //Sets the GUI skin
     GUI.skin = myGUIskin;    
         
         //If you click on this GUI buttn
         if(GUI.Button (new Rect(0, 0, 150, 30), "Start Game")) {
         
             Application.LoadLevel(1);    
             
         }
         
         //This GUI Box shows you what track you are at right now
         GUI.Box (new Rect(20, 30, 150, 30), "Track: " + Track);
         
         //If you click on this GUI buttn
         if(GUI.Button (new Rect(0, 30, 30, 30), "<")) {
             if(Track >= 1) {
             
                 Track -= 1;
                 
             }
             if (Track == 0) {
                 Track = NumberOfTracks;
             }
         }
         
         //If you click on this GUI buttn
         if(GUI.Button (new Rect(160, 30, 30, 30), ">")) {
             if(Track <= NumberOfTracks) {
             
                 Track += 1;
                 
             }
             if (Track == Number1More) {
                 Track = 0;
             }
         }
     }    
 }
 `

so yeah there it is and what I was hoping I could do was make it so where on void start the size of the array would change to "NumberOfTracks" and all you had to do was enter the GUItextures and then I would just set it up from there.. I hope you know what i mean

well thanks :D

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

Answer by Yokimato · Apr 17, 2013 at 03:40 PM

You can implement a function that grabs the new size of the array, makes a new one of that size, and the brings over the old array to the new one, and finally assigns the new one to the original variable..... (psst...don't do this, keep reading)

OR

You can take advantage of existing class that do this for you. If you import that namespace System.Collections.Generic you will have access to the List object which, if you're from the Java world, is pretty exact to ArrayList in that it's a collection that's size can expand without headache.

Here's how you could use it:

 Using System.Collections.Generic;
 
 public class YourClass {
    // instead of public int[] TrackPictures
    public List<int> TrackPictures;
   
    void Start() {
      TrackPictures.Add(1);
      TrackPictures.Add(314); // examples...
    }
 
 }

This avoids you having to reinvent the wheel and keeps your code specific that what it's supposed to do. Here's all what List can do: MSDN Documentation

Further: Unity has buit-in support for lists in the Editor view and will appear the same as an array does. So now you have no excuses :P

Comment
Add comment · Show 2 · 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 logang · Apr 17, 2013 at 04:41 PM 0
Share

I get this error:

Assets/Scripts/$$anonymous$$ain$$anonymous$$enu.cs(12,16): error CS0246: The type or namespace name List1' could not be found. Are you missing a using directive or an assembly reference?

Like I said I am new to C#

avatar image Yokimato · Apr 17, 2013 at 08:02 PM 0
Share

You're missing Line 1, the Using System.Collections.Generic;. It's equivalent to an import statement in java.

avatar image
0

Answer by logang · Apr 19, 2013 at 01:59 AM

Ohh yeah lol thanks. XD

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 Yokimato · Apr 19, 2013 at 02:15 AM 1
Share

glad it worked; In the future, I would keep these to comments rather than another answer. And, if it worked, mark it my answer as the solution so it can help future unity users.

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

12 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

Related Questions

how to put the GUI into a gameobject so that when clicked, The GUI pops up, and heres a sample multiple Choice question using GUI, i need the code for it. 1 Answer

C# Random String Array help 2 Answers

Arrays with zero length 1 Answer

Distribute terrain in zones 3 Answers

Replacing textures 0 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