- Home /
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]
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.
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
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?
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.
@thorskull check the two answers below and comment if either one helped (and tick it correct, if it did) :)
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.
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.
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?
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!
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;
Your answer
Follow this Question
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