Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Mr.Reaper · Apr 23, 2012 at 11:49 AM · arrayobjectsactivate

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mr.Reaper · Apr 23, 2012 at 01:35 PM 0
Share

This throughs all kinds of errors!

avatar image fafase · Apr 23, 2012 at 01:40 PM 0
Share

You need to say what kind of errors

avatar image Mr.Reaper · Apr 23, 2012 at 01:48 PM 0
Share

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

avatar image fafase · Apr 23, 2012 at 01:56 PM 0
Share

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.

avatar image Mr.Reaper · Apr 23, 2012 at 01:59 PM 0
Share

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

Show more comments
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Apr 23, 2012 at 12:13 PM 0
Share

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.

avatar image Comaleaf · Apr 23, 2012 at 02:30 PM 0
Share

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.

avatar image
0

Answer by zz74b · Apr 23, 2012 at 12:00 PM

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Apr 23, 2012 at 12:05 PM 0
Share

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.

avatar image zz74b · Apr 23, 2012 at 01:25 PM 0
Share

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.

avatar image fafase · Apr 23, 2012 at 01:30 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges