Why does my text doesn't stay on screen?
Hi! As you might imagine, I'm kinda new to this scripting, programming thing. I'm trying to create a text game ( You know, those old school games with only text on the screen?). I'm trying to make an if statement about a key or something. I want my text to change if the bool ''haskey'' is false.
I can see the text wants to change but it changes back to the original text. My script looks like that.
void State_OutsideCell1 () { hasKey1 = false; text.text = "you see a door and a key, press T to take the key or press D to open the door.";
// You have the key and you open the door
if (hasKey1 = = true && Input.GetKeyDown (KeyCode.D)) {
text.text = "You enter the door blablabla";
myStates = States.whatever;
}
// You don't have the key
if (Input.GetKeyDown (KeyCode.D) && hasKey1 == false)
text.text = "You don't have a key press blabla to blabla.";
}
I'm pretty sure it has something to do with the first line but I don't know how to change it so it's not THE line of the method.
Thanks for your help!
Hi, welcome to Unity Answers. Please have a look at the FAQ and the User Guide, so you can use this forum efficiently in the future. :)
Please make sure you edit your question using the little black wheel on the right and format it properly using the 101 010 button for code parts, so it is easy to read for others.
if (has$$anonymous$$ey1 = = true && Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.D))
..will not compile because of all the spaces between the "=="
I recommend you watch the basic scripting tutorials 1-3 and 5-9 here.
This should give you a stable knowledge on common Unity scripting tasks. ;)
Please watch them carefully and hopefully you can tell by yourself why your script is not working. If you have any trouble following them, feel free to ask, since you seem to be a beginner and willing to learn something ;)
Have a nice day.
Please format your code using the 101010 button - it's hard to see what the first line is you're talking about, but it looks to be has$$anonymous$$ey1 = false;
. So, you're setting has$$anonymous$$ey1 to be false every time this function is called, and there doesn't seem to be anywhere where you set it to true?
Answer by xavier12321 · Nov 25, 2015 at 03:23 PM
void State_OutsideCell1 () {
if (hasKey1 == false)
text.text = "You are in front of a door and there is a key to your left. \n\n Press o to open the door \nPress T to take the key.";
{
if (hasKey1 == true)
text.text = "You have a key in hand. press o to open the door";
else if (hasKey1 == true & Input.GetKeyDown (KeyCode.O))
myStates = States.door1;
else if(hasKey1 == false & Input.GetKeyDown(KeyCode.T))
text.text = "You don't have a key! Press T to take the key.";
hasKey1 = true;
}
}
I'm trying something like that. It just wont work. when I enter this ''Room'', it makes haskey1 true and the text is ''You are in front of a door and there is a key to your left (...) ''.
I'm pretty sure it has everything to do with the way I typed my if statements and the curly bracers.
Note: I made a way over complicated system to make this work but I don't want to use that. See for yourself.
/* void State_OutsideCell1 () {
text.text = "As you get closer to the door, it gets bigger. You think it is a bit intimidating. Your head starts to hurt like crazy. " +
"What is behind that door? To your left, there is a key on the wall.\n\n press T to take the key.\n\n press D to open the door. ";
if (hasKey1 == true && Input.GetKeyDown (KeyCode.D)) //
myStates = States.door1;
else if (Input.GetKeyDown (KeyCode.D) && hasKey1 == false) //
myStates = States.HasNoKey1;
else if (Input.GetKeyDown (KeyCode.T) && hasKey1 == false) //
myStates = States.Key1;
else if (Input.GetKeyDown (KeyCode.T) && hasKey1 == true) //
myStates = States.HasKey1;
}
void State_HasKey1 () {
text.text = "you already took the key and There is nothing left on the wall. Press B to go back";
if (Input.GetKeyDown (KeyCode.B))
myStates = States.OutsideCell1;
}
void State_HasNoKey1 () {
text.text = "You feel nauseous as you reach for the door knob. Why is that? The door is locked and you can't open it without a key." +
"Press B to go back";
if(Input.GetKeyDown(KeyCode.B))
myStates = States.OutsideCell1;
}
void State_Key1 () {
text.text = "Has you reach for the key, your head suddenly feels like it is going to explode. You take a deep breath and clear your mind." +
"\n\n press t to take the key";
hasKey1 = true;
if (Input.GetKeyDown (KeyCode.T))
myStates = States.OutsideCell1;
}
void State_Door1 () {
text.text = "You use the key on the lock and open the door. you shit on your chest. press anykey to masturbate.";
} */
it's just too long and stupid... But it works.... ^,^
Your answer
Follow this Question
Related Questions
null reference exception with text 0 Answers
Show a text in the position of a GameObject 0 Answers