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 thorskull · Jun 25, 2013 at 02:24 PM · arraysactivation

Turning off single object in an array

I have this code that lists an array of GUITextures, 16 in total. They all start disabled. What I want is that when I initially click the first GUI it activates the first object in my array; then when I click that GUITexture it is turned off and the next Texture in the array is turned on. I can turn them all off or on but for the life of me I can't figure how to turn individual ones on. I'm using C# too just fyi. Thanks!

[CODE] public class LegZoom2 : MonoBehaviour {

 public GameObject body;
 public GameObject player;
 public GameObject legcamera;
 public GUITexture backbutton;
 public GUITexture simtest;
 public GUITexture simpract;
 public GUIText simtext;
 public string[] myStrings; 
 public int currIndex;
 public GUITexture[] steps = new GUITexture [16];
 public int currIndexep;
 

 // Use this for initialization
 void Start () {
     
     
     Screen.showCursor = true;
 
 }
 
 
 // Update is called once per frame
 void Update () {
 
     Vector3 StartPos = legcamera.transform.position;
     Vector3 EndPos = new Vector3 (6.1f, 9.1f, -4.017f);

     
 if(((body.transform.position.x)-(player.transform.position.x)) <= 2.6f && (((body.transform.position.z)-(player.transform.position.z)) >= -1f||((body.transform.position.z)-(player.transform.position.z)) <=3.4f)) {
     
     legcamera.SetActive(true);
     player.GetComponent<AudioSource>().enabled = false;
     legcamera.transform.position = Vector3.Lerp (StartPos, EndPos, Time.deltaTime * 2);
     
     }
     
 else {
         legcamera.SetActive(false);
         legcamera.transform.position = new Vector3(4.54f, 9.434f, -3.932f);
         simtest.enabled = true;
         simpract.enabled = true;
         simtext.enabled = false;
         
         foreach (GUITexture obj in steps)
         {obj.active = false;
         }
         
     
     }
         
     if (simtest.HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0)){
         
         simtext.enabled = true;
         simtest.enabled = false;
         simpract.enabled = false;
     }
     
     if(simpract.HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0)){
         simtest.enabled = false;
         simpract.enabled = false;
         steps[0].enabled = true;
         
     }
         
     
     if (steps[0].HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0)){
         currIndexep++;
         steps[currIndexep].enabled = true;
         steps[currIndexep--].enabled = false;
     }
     
 myStrings= new string[]{"Lateral Incision", "Mark Incision", "Incise skin & \nSubcutaneous Tissue", "Perform 'H-cut'; \nwatch for peroneal nerve", "Lateral Incision Complete", "Medial Incision", "Mark Incision", "Expose and free Muscles", "Fasciotomy Complete"};
     
     if (simtext.HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0)){
         
         currIndex++;
         simtext.text = myStrings[currIndex];
     }
 
     }
 }

[/CODE]

Comment
Add comment · Show 5
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 RatStory · Jun 25, 2013 at 03:10 PM 1
Share

I'm unclear as to whether what you're trying to do is add some sort of functionality to the way the Inspector shows your script/array of GUITextures in edit mode, or something that should be happening at run time.

avatar image thorskull · Jun 25, 2013 at 03:22 PM 0
Share

It should happen at runtime; all should start off and then one at a time should turn on and off while I click to control them

avatar image RatStory · Jun 25, 2013 at 03:43 PM 0
Share

I'd never seen GUITextures themselves be turned on and off before, I'm not sure how that's supposed to behave. What code is responsible for making them show up on screen?

avatar image thorskull · Jun 25, 2013 at 04:16 PM 0
Share

All the parts in my code such as the "simtest.enabled = false;" or similar ones turn on or off the GUITextures. You just have to declare a public GUITexture at the beginning.

avatar image Em3rgency · Jun 25, 2013 at 04:37 PM 0
Share

@thorskull check the two answers below and comment if either one helped (and tick it correct, if it did) :)

2 Replies

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

Answer by Em3rgency · Jun 25, 2013 at 04:00 PM

I am assuming you're setting currIndexep in the inspector? Because in the code it always stays null. And null++ is still null.

Also there are some logic problems in

     if (steps[0].HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0)){
        currIndexep++;
        steps[currIndexep].enabled = true;
        steps[currIndexep--].enabled = false;
     }

First, you're only checking hits or steps[0] in the if statement. I assume this would cause the problem after the first click, as steps[0] would be disabled then, and steps[1] would be the active one.

Next, you should really read up on the ++ and -- some. currIndexep-- returns the value of currIntexep and THEN takes away 1. Also you don't want to take away 1. You want to access currIndexep - 1.

currIndexep-- is the same as currIndexep = currIndexep - 1; You don't want that here.

Additionally, you don't even need to do that, your whole sequence can be

 steps[currIndexep].enabled = false;
 currIndexep++;
 steps[currIndexep].enabled = true;

Finally, add a safety check. If currIndexep == theSizeOfYourArray - 1, you should manually set the last element to disabled and the first element to enabled and currIndexep to 0 agian.

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 thorskull · Jun 25, 2013 at 05:24 PM 0
Share

Yea I completely forgot about how -- was working. I changed it to the last snippet of code in your answer and it's working fine. The only problem I'm having is that when I initially try turning on the very first step or step[0]; it won't enable when I click the initial Texture. If I just turn them all on and click them off one by one the new code works but just this one little thing.

avatar image Em3rgency · Jun 25, 2013 at 08:45 PM 0
Share

I'm not sure what exactly you're doing here, but I'll ask again, shouldn't it be

 steps[currIndexep].HitTest(Input.mousePosition)

ins$$anonymous$$d of

 steps[0].HitTest(Input.mousePosition)

in the if statement?

avatar image thorskull · Jun 25, 2013 at 09:04 PM 0
Share

Yeah I changed that. I figured out how to make them all work though. So thanks so much, would not have figured out how to make that code work any time soon!

avatar image
0

Answer by AntiLunchBox · Jun 25, 2013 at 03:41 PM

It looks like actually in your code you are never leaving the first element:

    currIndexep++; //currindex goes from 0 to 1
    steps[currIndexep].enabled = true; //enables at index 1
    steps[currIndexep--].enabled = false; // currentindex goes from 1 to 0 JUST USE currIndexep-1.   "--" will apply the new value to currIndexep

It's really unclear what you are trying to do since your parentheses are not formatted correctly. However, this function will turn only one element on. It basically turns every element off and turns on the one you want:

 void TurnThisIndexOn(int index)
 {
     foreach(GUITexture step in steps)
     {
         step.enabled = false; //or .active or .activeSelf or .SetActive depending on ur version of Unity
     }
     steps[index].enabled = true;
 }

The second option is to keep track of which element it is currently on (with an int variable) and turn that index off before you turn on the next index with:

 steps[currentIndex].enabled = false;
 currentIndex = nextIndex;
 steps[currentIndex].enabled = true;
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

18 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Array empties values on RunTime? 1 Answer

transform array in javascript 2 Answers

Add a copy of a object to Array 1 Answer

Find colliders after collision? 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