|
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!
(comments are locked)
|
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. 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)
|
|
This is pseudo code, based on c# assuming:
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)
|
|
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 And the script which controls them: Then in the inspector you can set the value 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)
|
