About 'Functions & Encapsulating'!

In this video (objectives)…

  1. Create a new function for StartGame().
  2. Create a new function for NextGuess().
  3. Discussion encapsulating as it relates to C#.
  4. Finish our game.

After watching (learning outcomes)…

Create your first functions and finish off our simple game.

(Unique Video Reference: 8_NC_CUD)

We would love to know…

  • What you found good about this lecture?
  • What we could do better?

Remember that you can reply to this topic, or create a new topic. The easiest way to create a new topic is to follow the link in Resources. That way the topic will…

  • Be in the correct forum (for the course).
  • Be in the right sub-forum (for the section)
  • Have the correct lecture tag.

Enjoy your stay in our thriving community!

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

public class NumberWizard : MonoBehaviour
{
    int max;
    int min;
    int guess;

    // Start is called before the first frame update
    void Start()
    {
        IntializeStuff();
    }

    // Update is called once per frame
    void Update()
    {
        GetInput();
    }

    private void GetInput()//Check if the number is correct

    {
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            min = guess;
            NextGuess();
        }
        else if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            max = guess;
            NextGuess();
        }
        else if (Input.GetKeyDown(KeyCode.Return))
        {
            Debug.Log("You got it: \n\n");
            IntializeStuff();
        }
    }

    private void NextGuess()
    {
        guess = (max + min) / 2;
        Debug.Log("Is it higher or lower than: " + guess);
    }


    private void IntializeStuff()
    {
        max = 1000;
        min = 1;
        guess = Random.Range(min, max);
        PrintWelcome();
        max = max + 1;
    }

    void PrintWelcome()
    {
        Debug.Log("Hi All, time to play number wizard: ");
        Debug.Log("Pick a number: ");
        Debug.Log("The highest number is " + max);
        Debug.Log("The lowest number is " + min);
        Debug.Log("Is it higher or lower than: " + guess);
        Debug.Log("Push Up arrow for Higher, Down Arrow for Lower, and Enter for Correct");
    }
}

My version. Good to have a speed revision of unity. Its been a while

Quick question, hoping someone can shed some light here.

In this part of the code:

void StartGame()
{
max = 1000;
min = 1;
guess = 500;

Why is it that if you declare these local variables as integers again the output of the program is always zero?

I understand there isn’t need to declare them again (as recommended @ 10.41) and it would be cleaner not to do so, however this error stumps me and I’m not sure why it output to always return zero.

Sg

Privacy & Terms