[soled] i think Adding button

ok to i followed what was going on and did as you was saying but most the changer i cud not do on my one but i am complete noob but i under stand the layout but that not what i am hear for just that i let you know how inexsperans i am

on the Cours “Complete C# Unity Developer 2D: Learn to Code Making Games” i got to end of section 4 but then watched section 5,6 but did nothering at was watching at night and was tired but that i see if i cud pick anything up

ok on to my question sorry hear is my github https://github.com/duper3/text101-udemy

i am trying to add 5 buttons

  1. opshon 1 / 2. opshon 2 / 3. opshon 3 /
  2. restart ( this 1 ment to tack you back to start
  3. quit (this 1 i have worked out and put in under quit.cs

it button 4 most i wood like help with
button 1,2,3 wood be nice

thir is a lot of other stuff i whant to do to this to make it feel like a advencher but i will try adding them later

i got it 1s where it was saying somthing about "null cant find object" you can see some of my trys but i have tried looking and i think 10 - 20 different ways i seen 1 on hear but that look like the old way you did state

thz for help o and on the quit.cs this works Debug.Log(“restart button press”);

ps. as i have you hear witch Cours wood be good for 2d turnbace rpg space game

and i doing know how to making that you put coad on hear and it hides it then you click it to see it

Hi,

Did you connect your buttons to a script connected with a game object? See what we did in the Number Wizard UI section.

NullReferenceException means that a reference (“link”) to an instance is missing. If you exposed a field in the Inspector, make sure that it’s not empty.

i have a empty object that i called buttons that i attached the quit scrip to then click on it and asine the quit button to button 1 and my reset button to button 2 i have put a pic of my 2 buttons and my object the scrip attached to

when i press button bank ( my restart button) i get the debug you press the button

think the problem i am having is using State and telling it to go to set State

sorry if i am confusing you but thank you for reply

some how i got the button to work to reset back to start but i disabel 1,2,3 and made r,t,y work

in my quis.cs i add

    void Start()
    {
        state = gameStart; // add
        textComponent.text = state.GetStateStory(); // add

        ButtenQuit.onClick.AddListener(() => buttenquit());
        Restart.onClick.AddListener(() => restart());
    }

    void Update()
    {
        KeyQuit();
        Key();  // taken the // off
        //restart();
    }
// add all below

 private void restart()
    {
        Debug.Log("restart button press");
        var nextStates = state.GetNextStates();
        Debug.Log("GetNextStates");
        state = gameStart;
        Debug.Log("state = gameStart");
        textComponent.text = state.GetStateStory();
    }

   private void Key()
    {
        var nextStates = state.GetNextStates();
        if (Input.GetKeyDown(KeyCode.R))
        {
            state = nextStates[0];
        }
        else if (Input.GetKeyDown(KeyCode.T))
        {
            state = nextStates[1];
        }
        else if (Input.GetKeyDown(KeyCode.Y))
        {
           state = nextStates[2];
        }
        textComponent.text = state.GetStateStory();
    }

but if i remove the button dosent work at all and the 1,2,3 works againe

   private void Key()
    {
        var nextStates = state.GetNextStates();
        if (Input.GetKeyDown(KeyCode.R))
        {
            state = nextStates[0];
        }
        else if (Input.GetKeyDown(KeyCode.T))
        {
            state = nextStates[1];
        }
        else if (Input.GetKeyDown(KeyCode.Y))
        {
            state = nextStates[2];
        }
        textComponent.text = state.GetStateStory();
    } 

You connected your ButtenQuit object with the restart() the buttenquit method, and the Restart object with the restart() method.

What do you expect to happen? And what happened instead?

In the restart method, you first fetch the next states of the current state. Then you assign gameStart to state. Then you pass on the string of the state to textComponent.text. You do not do anything with nextStates.

so i think i figerd out how this working the “void Start()” is inishinising for this scrip and “private void restart()” is changing it but not updating the change then " void Update() updates Key();" witch input the change

 void Start()
    {
       state = gameStart;
    }

   void Update()
    {
        Key();
    }
    private void restart()
    {
        state = gameStart;
    }

 private void Key()
    {
        var nextStates = state.GetNextStates();
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            state = nextStates[0];
        }
        
        else if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            state = nextStates[1];
        }
        else if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            state = nextStates[2];
        }
        textComponent.text = state.GetStateStory();        
    }

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms