x


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!

more ▼

asked Apr 23 '12 at 11:49 AM

Mr.Reaper gravatar image

Mr.Reaper
2 1 3 4

(comments are locked)
10|3000 characters needed characters left

3 answers: sort oldest
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.

more ▼

answered Apr 23 '12 at 12:03 PM

fafase gravatar image

fafase
11k 11 15 45

This throughs all kinds of errors!

Apr 23 '12 at 01:35 PM Mr.Reaper

You need to say what kind of errors

Apr 23 '12 at 01:40 PM fafase

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; }

Apr 23 '12 at 01:48 PM Mr.Reaper

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.

Apr 23 '12 at 01:56 PM fafase

It says that Length is not a member of function(int): void

Apr 23 '12 at 01:59 PM Mr.Reaper
(comments are locked)
10|3000 characters needed characters left

This is pseudo code, based on c# assuming:

  1. your object to 'activate' has an 'Activate' method that thats no args.
  2. your objects are stored in an array called 'arrayOfObjects'
  3. your 'int' is called 'numToActivate'
for(var i = 0; i < Math.Min(numToActivate, arrayOfObjects.Length); i++)
{
   arrayOfObjects[i].Activate();
}
more ▼

answered Apr 23 '12 at 12:00 PM

zz74b gravatar image

zz74b
98 2 3 5

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.

Apr 23 '12 at 12:05 PM fafase

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 programming principals, so the code above should give them something to work with before going off and doing more of their own research.

Apr 23 '12 at 01:25 PM zz74b

true, well all in all he should have all he needs within the three answers.

Apr 23 '12 at 01:30 PM fafase
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Apr 23 '12 at 12:07 PM

Comaleaf gravatar image

Comaleaf
83 5 7 7

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.

Apr 23 '12 at 12:13 PM fafase

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.

Apr 23 '12 at 02:30 PM Comaleaf
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1396
x326
x81

asked: Apr 23 '12 at 11:49 AM

Seen: 539 times

Last Updated: Apr 23 '12 at 02:30 PM