Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by arthurconnor100 · Aug 01, 2017 at 03:28 PM · if statement

If statement not starting

I am currently working on a text adventure game that is about escaping a prison, but when I tested the code, nothing happened when I pressed L or R. Any help would be greatly appreciated.

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class TextController : MonoBehaviour {

public Text text;

private enum State {knife, mirror, pillow, sheet, cell, bolt};

private bool Knife = false;

private State mystate;

 // Use this for initialization
 void Start () {
     text.text="You are a prisoner, in a cell, at a low security prison.  " +
                "In your cell you have a hand held mirror, a makeshift knife, " +
                "and a pillow on your bed.  The lock to your cell is a rusty barrel bolt " +
                "that is very loose\n\n" + 
                "Press z to pick up the pillow, k to pickup the knife, and m to pickup the mirror";
 }
 
 // Update is called once per frame
 void Update ()
 {
     if (mystate == State.cell) {
         text.text="You are a prisoner, in a cell, at a low security prison.  " +
                "In your cell you have a hand held mirror, a makeshift knife, " +
                "and a pillow on your bed.  The lock to your cell is a rusty barrel bolt " +
                "that is very loose\n\n" + 
                "Press p to pick up the pillow, k to pickup the knife, and m to pickup the mirror";
     }

     if (Input.GetKeyDown(KeyCode.K)) {

     knife();
     mystate = State.knife;
 }
 }

 void knife ()
 {
     if (Knife == false) {

         text.text = "As you inspect your knife you notice it has a very long blade.\n\n" +
         "Press L to put the knife in your pocket or press R to leave it where it is";
     }
     else if (Knife == true) {

         text.text = "As you inspect your knife you notice it has a very long blade.\n\n" +
         "Press R to leave it where it is";
     }
     if (Input.GetKeyDown (KeyCode.L)) {
         
         Knife = true;
         mystate = State.cell;
     }
     if(Input.GetKeyDown (KeyCode.R)) {
       mystate = State.cell;
     }
 }

}

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

1 Reply

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

Answer by NoseKills · Aug 01, 2017 at 03:56 PM

The if conditions work fine. You just never end up executing them.

 if (Input.GetKeyDown(KeyCode.K)) {
      knife();
      mystate = State.knife;
  }

Your knife() method gets called once during the frame where the K key becomes pressed. The other 2 input checks are done inside knife(), so they will only be true if you press K and L at the exavt same moment.

Also if you plan to expand on this logic, i recommend you clarify the logic of this state machine or it'll get out of hand soon.

You could for example make a big switch-case in update where you check for input and call a ChangeState() method based on the current state and pressed key. In your ChangeState() method, set the text to be what it needs to based on the state that becomes active. I.e. do input chcking always in the same spot and setting the text in another. Now you set some text in Start, some in Update and some in knife()

 void Update() {
     switch (mystate)
     {
         case State.cell:
              if (Input.GetKeyDown(KeyCode.K)) {
                  ChangeState(mystate, State.knife);
              }
         break;
         case State.knife:
              if (Input.GetKeyDown(KeyCode.L)) {
                  ChangeState(mystate, State.cell);
              }
         break;
     }
 }

 void ChangeState(State oldState, State newState) {
         // Add parameters if you need to know more about game state to do the right things here
         mystate = newState;
         //Set text based on new state & do other stuff you know should happen when doing this state transition.
 }


You could also put all your text in a Dictionary<State, string> and/or in a completely separate class so it doesn't bloat this class. Then you could just do

 text.text = yourDictionary[newstate];

to get the correct text in ChangeState

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

68 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 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 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 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

Zombie Attack Animation script 1 Answer

How to use or statements in an if statement. 1 Answer

how to reset jump everytime it hits ground not when it is constantly on the ground 0 Answers

If & Else If with == and && not working 0 Answers

Unexpected token: if HELP 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