Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

== not working

Discussion in 'UGUI & TextMesh Pro' started by Yongtheskill, Jan 28, 2017.

  1. Yongtheskill

    Yongtheskill

    Joined:
    Jan 28, 2017
    Posts:
    1
    Hi, I am not very experienced in Unity, and I am trying to create a game.
    It is supposed to be such that if you type start in an input field and press enter, it brings you to the next scene.
    However, when I type start into the input field and press enter, nothing happens.
    If I change == to != and type start, it still goes to the next scene.

    Here is my code:

    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.SceneManagement;

    public class StartMenu : MonoBehaviour {

    private GameObject command;
    private string Command;
    private LevelManager levelManager;

    // Use this for initialization
    void Start () {
    Command = command.GetComponent<InputField>().text;
    }

    public void Enter(){
    print("Game Started");
    SceneManager.LoadScene("Game");
    }


    void Update () {

    if (Input.GetKeyDown(KeyCode.Return)){
    if (Command != "start"){
    Enter();
    }
    }
    }
    }

    Please help me.
    Thanks in advance.
     
  2. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    4,381
    DanielQuick likes this.
  3. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    You are setting the value of Command once when the game begins. You should be setting it directly before checking the value.

    Code (csharp):
    1. if (Input.GetKeyDown(KeyCode.Return)) {
    2.     Command = command.GetComponent<InputField>().text;
    3.     if(Command == "start") {
    4.  
    5.     }
    6. }