hey all, i apologize in advance for the simple question but i am not a programmer and i can’t seem to understand arrays.
i would like to setup an array for gameobjects. let’s say i have a capsule, a box and a sphere. how would i set up an array for the objects and some gui buttons.
i want to only render one object at a time (in a sense cycling through the array one object at a time).
press gui button 1 and see the first object. press button 2 for the second object etc?
i have read the manual and the forums but cant even seem to get started. a basic example would be very helpful.
Then simply attach this file to an object in the game and then in the inspector choose how many objects you want it to contain and then drag each object onto it.
Alternatively
var objs : GameObject[];
var objsUsed : int = 0;
function Start()
{
objs = new GameObject[3];
}
function AddObject(obj : GameObject)
{
if (objsUsed == objs.length)
{ Debug.Log("All spaces filled");} else
{
objs[objsUsed] = obj;
objsUsed++;
obj.renderer.enabled = false;
}
}
var toshow : int;
function OnGUI()
{
for (x=0; x < 3; x++)
{
if (GUI.Button())
{
objs[toshow].renderer.enabled = false;
toshow = x;
objs[toshow].renderer.enabled = true;
}
}
}
Okay, so this is entirely untested, but it should give you a clue
The thing with .NET arrays is that you have to recreate them when you want to extend it. Normal arrays don’t have that problem but you can’t give it a type…
i.e.
var objs : Array();
function AddObj(obj : GameObject)
{
objs.Push(obj);
}
function HideAll()
{
var obj : GameObject;
for (x = 0; x < objs.length; x++)
{
obj = objs[x];
obj.renderer.enabled = false;
}
}
One thing I an unsure of is if you do this:
var objs : GameObject[];
function Start()
{
objs = new GameObject[3];
}
function Extend()
{
objs = new GameObject[5];
}
I don’t think the extend function will retain what is in objs. I think it will delete everything. So I tend to use [ ] arrays only when I know for sure how many elements an array will have at design time and use () arrays the rest of the time…
If someone could verify this for me, I would be most grateful…
i was able to use part of your example. thanks for the input. you got me going in the right direction.
here is what i ended up using. as you said, you have to assign the variables in the inspectors and this gives me what i wanted
thanks for your help.
var objs : GameObject[];
var objsUsed : int = 0;
var toshow : int;
function OnGUI()
{
for (x=0; x < 3; x++)
{
if (GUI.Button(Rect(315,710,150,50), "view A"))
{
objs[toshow].renderer.enabled = false;
toshow = 0;
objs[toshow].renderer.enabled = true;
}
}
if (GUI.Button(Rect(500,710,150,50), "view B"))
{
objs[toshow].renderer.enabled = false;
toshow = 1;
objs[toshow].renderer.enabled = true;
}
if (GUI.Button(Rect(700,710,150,50), "View C"))
{
objs[toshow].renderer.enabled = false;
toshow = 2;
objs[toshow].renderer.enabled = true;
}
}
there are some things you should change in that code, though…
var objs : GameObject[];
var toshow : int;
private var names :Array = new Array("View A", "View B", "View C");
function OnGUI()
{
//this makes it more portable. now you can just assign more in the inspector and it will still work
for (x=0; x < objs.length; x++)
{
if (GUI.Button(Rect(115 + (x * 200) ,710,150,50), names[x]))
{
objs[toshow].renderer.enabled = false;
toshow = x;
objs[toshow].renderer.enabled = true;
}
}
}
The For loop above already creates 3 buttons. Your code had the program draw 3 buttons on top of each other and then 2 more next to it. The code above does the exact same as your example. I removed the objsused variable since you didn’t use it and where you hardcoded 3 (this is where you drew the button 3 times, instead of 3 buttons) I made it more dynamic.