- Home /
GameObject Array
Hello,
I would to walk up to an object in my scene (first person controller), click on a sphere for arguments sake and a new object/cube will appear in it's place, then if this is clicked another new object/cylinder will appear. Finally I would like to click on the cylinder and it will loop around back to the sphere, it's key this cycle continues. I have been trying to use Array's for this, I have successfully created a material array (from pieces of acquired script) but I have not been as successful for Object Arrays.
I hope this is clear, this is what I have in the way of script to begin with:
var arr : GameObject[];
private var index : int;
function OnMouseUp ()
Your help will be greatly appreciated :)
Answer by Berenger · May 21, 2012 at 02:46 PM
In that case, the script inplementing OnMouseUp would be attached to the sphere and the cylinder. You should add a variable called something like next. When the message OnMouseUp is received, you can deactive this object and active next.
Thank you for the speedy reply, I am still pretty new to all of this (3weeks) and scripting is not my strong point. There appears to be many ways to do what I need to do, some people have suggested using the Instantiate command, others suggest using an Index, I am still unsure which method to use. Or if to use both!
From your comment Berenger I have gathered that I need to input a new variable, but what script do I need to deactivate/active said object and how can I repeat said process?
For such a simple task, you don't need to care about performance. Instantiate is just as good as activate / deactivate then. Anyway, there is always a lot of solution for each problems. There is a lot of parameters to consider to choose the good one, such as performance, how much times it takes to implement, the coherence with the rest, and finally the programmer's taste ^^ (non-exhaustive list).
Anyhow, the toggle between the name and the icon of a gameObject in the inspector is the variable active, accessible from code. All the components are disabled when the gameobject holding them is deactivated. Here is what I had in $$anonymous$$d :
var next : GameObject; // The cylinder for the sphere, the sphere for the cylinder.
function On$$anonymous$$ouseUp() { gameObject.active = false; next.active = true; }