c# unity if keycode和if gold >买这个

本文关键字:if unity keycode gold | 更新日期: 2023-09-27 18:17:57

我是c#和unity的新手。除了我的代码非常混乱。为什么当我按1、2或3时,我的商店场景不能工作?这款游戏是基于文本的,到目前为止,除了这个,其他一切都行得通。

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class TextController : MonoBehaviour 
{
    public Text text;
    private enum States {forest, forest_explore, forest_stay, forest_stay2, forest_explore2, village, village_shop, village_shopping, village_talk};
    private States mystate;
    public Text healthtxt;
    public Text goldtxt;
    public Text damagetxt;
    public Text armourtxt;
    public Text notify;
    public int health = 20;
    public int damage;
    public int goldnmb = 30;
    public int armour;
    // Use this for initialization
    void Start () 
    {
        goldtxt.text = "Gold: 30";
        healthtxt.text = "Health: 20/20";
        damagetxt.text = "Damage: ";
        armourtxt.text = "Armour: ";
        mystate = States.forest;
    }
    // Update is called once per frame
    void Update () 
    {
        if(mystate == States.forest) // BEGINNING FOREST
        {
            state_forest();
        }
        else if (mystate == States.forest_explore) //EXPLORATION IN FOREST 1 STATEMENT
        {
            text.text = "You explore the darkness and find a rusted broadsword on the dry ground"+
                "'n'nWith your new sword. Do you stay the night or continue exploring"+
                    "'n(press z to explore or x to stay where you are)";
            notify.text = "You Got the rusted broadsword!";
            damagetxt.text = "damage: " + damage.ToString();
            damage = 3;
            state_forest2();
            if(mystate == States.forest_explore2) //EXPLORATION IN FOREST 2 STATEMENT
            {
                text.text = "You've been exploring for hours and you see a small light in the distance"+
                    ". When you reach the light you see a person. you walk up to them and ask where the nearest town is"+
                        ".'nHe points north and says 'Be careful out there. Things can be dangerous with out a light'"+
                        "'nYou tell him that you'll be fine and eventually reach the town."+
                        "'nYou sleep at an inn.";
                mystate = States.village;
            }
            else if(mystate == States.forest_stay2) //STAY IN FOREST 2
            {
                text.text = "You wake up in an inn. The innkeeper says'n 'What are you stupid! Staying outside without a light. Idiot...'";
                mystate = States.village;
            }
        }
            else if (mystate == States.forest_stay) // STAY IN FOREST 1
            {
                text.text = "You hear deep groans and high pitched laughter from the darkness"+
                    "'n'n'n"+
                    "!!!GAME OVER!!!";
            }
        if(mystate == States.village) //VILLAGE SCENE
        {
            state_village();
            if(mystate == States.village_shop) //VILLAGE BUYING ITEM
            {
                text.text = "A dagger costs 10 gold and does 10 damage. "+
                    "'n A sword costs 25 gold and does 15 damage. "+
                        "A battle axe costs 40 gold and does 25 damage. "+
                        "'n (press 1 for dagger, 2 for sword or 3 for battle axe)";
                state_shopping();
            }
            else if(mystate == States.village_talk) //VILLAGE TALKING
            {
            }

        }
    }

    void state_forest()
    {
        text.text = "You wake up in the harsh, cold, darkness of midnight with no weapons or equipment" +
            " (press z to explore or x to stay where you are)";
        if(Input.GetKeyDown(KeyCode.Z))
        {
            mystate = States.forest_explore;
        }
        else if(Input.GetKeyDown(KeyCode.X))
        {
            mystate = States.forest_stay;
        }
    }
    void state_forest2()
    {
        if(Input.GetKeyDown(KeyCode.Z))
        {
            mystate = States.forest_explore2;
        }
        else if(Input.GetKeyDown(KeyCode.X))
        {
            mystate = States.forest_stay2;
        }
    }
    void state_village()
    {
        text.text = "You walk outside and it's still dark you think to yourself. 'Where you asleep all day?' "+
            "everyone is carrying a lantern but you. You see a sign that says weapons. 'nDo you talk to the villagers or buy equipment? "+
                "(press q to buy weapons or w to talk to the villagers. You can return later.)";
        notify.text = "";
        if(Input.GetKeyDown(KeyCode.Q))
        {
            mystate = States.village_shop;
        }
        else if(Input.GetKeyDown(KeyCode.W))
        {
            mystate = States.village_talk;
        }
    }
    void state_shopping()
    {
        if(Input.GetKeyDown(KeyCode.Keypad1) && goldnmb > 10)
        {
            goldnmb = goldnmb - 10;
            goldtxt.text = "Gold: " + goldnmb.ToString();
            damage = 10;
            damagetxt.text = "Damage: " + damage.ToString();
        }
        else if(Input.GetKeyDown(KeyCode.Keypad2) && goldnmb > 25)
        {
            goldnmb = goldnmb - 25;
            goldtxt.text = "Gold: " + goldnmb.ToString();
            damage = 15;
            damagetxt.text = "Damage: " + damage.ToString();
        }
        else if(Input.GetKeyDown(KeyCode.Keypad3) && goldnmb > 40)
        {
            goldnmb = goldnmb - 40;
            goldtxt.text = "Gold: " + goldnmb.ToString();
            damage = 25;
            damagetxt.text = "Damage: " + damage.ToString();
        }
    }
}

c# unity if keycode和if gold >买这个

我打赌你在使用你的alpha键,甚至不知道它

试试这个:

if((Input.GetKeyDown(KeyCode.Alpha1)
   || Input.GetKeyDown(KeyCode.Keypad1))
   && goldnmb > 10)

编辑:state_shopping怎么能在这里运行?mystate必须同时等于States.villageStates.village_shop

if(mystate == States.village) //VILLAGE SCENE
{
    state_village();
    if(mystate == States.village_shop) //VILLAGE BUYING ITEM
    {
        text.text = "...";
        state_shopping();
    }