- Home /
Activate objects in array based on value
Hi, I have an array containing 5 objects, all of which inactivated. These should then activate based upon the value of an int variable. If the int = 3, the three first objects should be activated. If int = 1, only the first one should be activated and so on. This int value will change during gameplay and so the objects needs to be able to dynamically adapt recordingly. How would I go about doing this? Thankful for any help!
Answer by fafase · Apr 23, 2012 at 12:03 PM
var arrGo:GameObject[];
var wrongamount:int;
var wrongamount2 :int;
function Update(){
if(wrongamount!=wrongamount2){
LampActivation(new);
wrongamount=wrongamount2;
}
}
function LampActivation(wrongamount2:int){
for(int i = 0; i< wronglamp.Length; i++){
wronglamp[i].active = false;
}
for(int i = 0; i< wrongamount2; i++){
wronglamp[i].active = true;
}
}
I first deactivate them all and reactivate the needed one. Now my issue is where is this going to be attached. You will have to access the array differently depending where you put that one. But the process should be as such I guess.
I'm using this:
function Update () {
if(wrongamount != wrongamount2){ LampActivation (wrongamount); wrongamount2 = wrongamount; }
function LampActivation (wrongamount: int) { for(int i=0;< LampActivation.Length; i++) wronglamps[i].active = false; for(int i=0;< wrongamount; i++) wronglamps[i].active = true;
}
Yep my bad the declaration of the for loop is C style...should be (var i:int=0;i<...) also, as I said, where do you attached that script because if you put it into an object that has no access to it it won't find it. if you attach it to an empty game object with the object as children then you need to fetch the component with GetComponentInChildren that will return an array of children that you can access and altered.
It says that Length is not a member of function(int): void
Answer by Comaleaf · Apr 23, 2012 at 12:07 PM
You could have two components, one is a script that goes on the objects you want to be activated/deactivated, the other manages these components.
On the objects have a script called ObjectController
(or whatever):
var activated = false;
function Activate() {
// do whatever you need to do to activate
}
function Deactivate() {
// do whatever you need to do to deactivate
}
And the script which controls them:
var numActivated:int = 0;
var objects = new ObjectController[5];
function Update() {
var i = 0;
// activate any unactivated ones that need to be activated
for(; i < numActivated; i++) {
if (!objects[i].activated)
objects[i].Activate();
}
// deactivate the rest if they are activated
for(; i < objects.length; i++) {
if (objects[i].activated)
objects[i].Deactivate();
}
}
Then in the inspector you can set the value numActivated
(or change it programmatically at runtime). You can also drag the objects you want to be activated into objects
so long as they have an ObjectController
script attached.
I give you a +1 for the loop principle, though I would put them in a function to save a little resources. There is no need to run the 2 loops if the value is unchanged.
Yes, that would be better. I will also note that doing that will remove the ability to set the number in the Inspector, however that was not a stated requirement anyway.
Answer by zz74b · Apr 23, 2012 at 12:00 PM
This is pseudo code, based on c# assuming:
your object to 'activate' has an 'Activate' method that thats no args.
your objects are stored in an array called 'arrayOfObjects'
your 'int' is called 'numToActivate'
for(var i = 0; i < Math.Min(numToActivate, arrayOfObjects.Length); i++) { arrayOfObjects[i].Activate(); }
In your code, if he has 4 objects on, and then he only needs 3, you still have the fourth one on. I would think you first have to make sure they all get off and then put the needed ones back on.
He specified in the question that all his objects in the array start deactivated. So the code above does work for the scenario posed.
It seems likely that the OP lacks certain understanding of basic program$$anonymous$$g principals, so the code above should give them something to work with before going off and doing more of their own research.
true, well all in all he should have all he needs within the three answers.
Your answer
Follow this Question
Related Questions
Instantiate an array of prefabs? 0 Answers
Trying to be more efficient. Advice please... 1 Answer
How do you debug arrays of objects? 1 Answer
SAVE OBJECTS IN ARRAY 1 Answer
Deactivate/activate objects depending on player position 2 Answers