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 paco morales · Feb 11, 2013 at 12:45 PM · setactivevar

Set Active Three objects?

Hi everyone,

I'm trying to show one object at a time between three, every time I press one button.

Now I'm using to activate it the function "SetActive", with a static var from another script.

But now I can only switch between two objects...

Please take a look to this little short video.

Link Video

Please Take a look to my Script.

Thanks for you help.

 var tool01 : GameObject;
 var tool02 : GameObject;
 var tool03 : GameObject;
  
 
 function Update () {
 if(animations .changeGo == 1){ 
 tool01.SetActive(!tool01.activeInHierarchy);
 tool02.SetActive(!tool02.activeInHierarchy);
 tool03.SetActive(!tool03.activeInHierarchy);
 animations . changeGo = 0;
     }
 } 
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

Answer by Berenger · Feb 11, 2013 at 01:39 PM

Set everybody inactive, then activate the one you actually want active. Brutal, but efficient.

Comment
Add comment · 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
0

Answer by Dave-Carlile · Feb 11, 2013 at 01:46 PM

If I'm understanding right, you're trying to activate objects in sequence, the next one activated each time you press a button, looping back to the first once you've activated all 3, and only one active at a time? Kind of like radio buttons?

One way to do this is to have an integer value that identifies which item is active. Each time you press the button, increment the value by 1. Once you've reached the highest value, set the value back to 0.

I'm going to do the code examples in a compbinatoin of C# and pseudocode...

 // declare in the class, or at the top of the script in Javascript
 int activeObject = 0;


 // in the button press code
 if (button is pressed)
 {
   // increment the active object, wrapping around to 0
   activeObject++;
   if (activeObject >= max_number_of_objects)
   {
     activeObject = 0;
   }


   // here's another way to do this that eliminates the conditional logic
   // just pick one of them - don't do them both
   // look up the modulus operator (%)
   activeObject = (activeObject + 1) % max_number_of_objects;

   ActivateObject(activeObject);
 }

 // activate the selected object
 void ActivateObject(int activeObject)
 {
   switch (activeObject)
   {
     case 0:
       tool01.SetActive(true);
       tool02.SetActive(false);
       tool03.SetActive(false);
       break;

     case 1:
       tool01.SetActive(false);
       tool02.SetActive(true);
       tool03.SetActive(false);
       break;

     case 2:
       tool01.SetActive(false);
       tool02.SetActive(false);
       tool03.SetActive(true);
       break;
   }
 }


Or something like that. A better way to deal with the 3 objects would be to store them in an array and use activeObject as an index into the array. You can activate that one and deactivate all the others that way with simple function that will handle any number of objects.

Comment
Add comment · 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
0

Answer by kuivalappa · Mar 13, 2017 at 09:05 AM

well i think this is little too late but

public class TestGun : MonoBehaviour {

 public GameObject[] gun;
 public int activeIndex = 0;


 public void SetActiveObject(int aIndex)
 {
     activeIndex = aIndex;
     for (int i = 0; i < gun.Length; i++)
         gun[i].SetActive(i == activeIndex);


 }

 void Update()
 {


     SetActiveObject(activeIndex);
 }

 public void UpgradeButtom()
 {

     gun[activeIndex].SetActive(true);

     activeIndex++;
 }

}

Comment
Add comment · 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

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

12 People are following this question.

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

Related Questions

physics.OverlapSphere colliders 1 Answer

accessing a var in the script of one object from the script of another object 3 Answers

Changing texture on mouse click 1 Answer

load specific element of animation via var 2 Answers

Getting string to int then var 1 Answer


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