- Home /
How to make if/else statements progress in order
I have a GUIText that I am clicking that starts out with the text "Lateral Incision". What I'm aiming for is making it so that every time I click the text, the text changes to the next scripted statement. The problem now is that it starts and immediately jumps to the end. I haven't found anything helpful in other answers so was just wondering if someone knew what to do? I'm thinking it's something quite simple, but I can't figure it out.
Here's the code: if (simtext.HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0)){
simtext.text = ( "Mark Incision");
}
if (simtext.HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0)){
simtext.text = ("Incise Skin & \nSubcutaneous Tissue");
}
if (simtext.HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0)){
simtext.text = "Perform 'H-cut'; \nwatch for peroneal nerve";
}
if (simtext.HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0)){
simtext.text = "Lateral Incision Complete";
}
if (simtext.HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0)){
simtext.text = "Medial Incision";
}
if (simtext.HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0)){
simtext.text = "Mark Incision";
}
if (simtext.HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0)){
simtext.text = "Expose & Free Muscles";
}
if (simtext.HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0)){
simtext.text = "Fasciotomy Complete";
}
Answer by HenryStrattonFW · Jun 20, 2013 at 07:04 PM
This will just make your text read the final value. That is because every condition is the same, so all of them will fire as the code runs down them.
What i would suggest is this, instead of hard coding the text values into the statements, put them into an array. then store a "currentIndex" which will start at 0. Then when the button is pressed you increase the index and set the text. here is an example.
var myStrings = new Array("String 1","String 2","String 3");
var currIndex = 0;
function Update()
{
if(myGUIText.HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0))
{
currIndex++;
// here you will want to also check current index to either
// wrap it back around to 0 or clamp it on the last value
// based on the array length.
myGuiText.text = myStrings[currIndex];
}
}
That would work great if I were using JS but for the project I can only use C# (it's a requirement). Is there anything comparable to this for C#?
Don't use the JS Array class even in Unityscript. It's slow, untyped, and obsolete. Use built-in arrays or generic Lists. In this case it should be
var myStrings = ["String 1", "String 2", "String 3"];
which can be done in C# but with different syntax.
Is there anyway you could provide me with an example? I've only been working with Unityscript for the past week and a half so I'm not familiar with the more complex commands in C#. If you could I'd really appreciate it.
my apologies for some reason i thought you were using javascript which looking back at your post im not sure why I thought that, i always go C# myself..
only needs $$anonymous$$or changes for that just declare the array and index with the appropriate syntax
public string[] myStrings = new string[]{ "string one", "string two", "etc" };
public int currIndex = 1;
oh and function update() becomes void Update()
sorry for the mistake
Thank you so much! I had one small problem where it was skipping my first string, I had to move my string script into the Update for it to be recognized. But besides that it works great!