- Home /
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
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
I get this error:
Assets/Scripts/$$anonymous$$ain$$anonymous$$enu.cs(12,16): error CS0246: The type or namespace name List
1' could not be found. Are you missing a using directive or an assembly reference?
Like I said I am new to C#
You're missing Line 1, the Using System.Collections.Generic;
. It's equivalent to an import statement in java.
Answer by logang · Apr 19, 2013 at 01:59 AM
Ohh yeah lol thanks. XD
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
Follow this Question
Related Questions
C# Random String Array help 2 Answers
Arrays with zero length 1 Answer
Distribute terrain in zones 3 Answers
Replacing textures 0 Answers