About 'Scope And Context Of Variables'!

In this video (objectives)…

  1. Discuss how C# is organised / grouped.
  2. Address the problem of scope and with our variables not being accessible.
  3. Create a guess variable.

After watching (learning outcomes)… Be cool with how scope applies to declaring and using variables.

(Unique Video Reference: 6_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!

4 Likes

My intro message:

void Start()
    {
        string logMessage = "Welcome to Number Wizard\n" +
            "==========================================================================\n" +
            "Pick a number from " + minNumber + " through " + maxNumber + " and I'll try to guess your number.\n" +
            "I'll choose a number and you tell me if it's too high, too low, or correct\n" +
            "Press [space] when you pick a number and I'll start guessing\n" +
            "--------------------------------------------------------------------------";

        Debug.Log(logMessage);
    }

I’ve been programming for over 40 years with C/C++/Java/PHP(yes, I programmed with punched cards) so this isn’t exactly how you set it up in the lecture … old habits are hard to break.

3 Likes

My Greeting from Texas
Howdy Pardner, Welcome to Number Wizard

My Intro Message:

Greetings from The US, I have started this course to better understand C#, I’ve been using Unity for a little while and have developed and publish a couple games to the Google Play Store. I’m enjoying the course thus far and, i am glad to be learning about C# as it takes me awhile to finish a project figuring out the code as i go along. I have a pretty good understanding of Unity just lack a strong understand of the C# code.

2 Likes
    Debug.Log("Habari Yako Karibu kwa wizard Numba. ");
    Debug.Log("Pick a number, don't tell me what it is...");
    Debug.Log("Not more than: " + max );
    Debug.Log("and not less than " + min + "lets Start" );
    Debug.Log("State if the number lower or higher than: " + guess);
    Debug.Log("Push Up = Higher");
    Debug.Log("Push Down = Lower");
    Debug.Log("Push Enter = Correct");

From Mombasa Kenya enjoying the course

I got a game who doesn’t treat you that good :smiley:
Also put a welcome in the langage from Britanny (Oh god how do you call that ? Can’t be “british” hahaha)

Debug.Log(“Oh god, here comes another one! Degemer Mat!”);
Debug.Log(“If you had to choose one number. What would it be ?”);
Debug.Log(“Of course you wouldn’t pick something higher than. " + max + " Who would?”);
Debug.Log("Neither would this number be lower than " + min + “. Now that would be really silly. Wouldn’t it ?”);
Debug.Log(“Tell me if your number is lower than 500”);
Debug.Log(“Push Up = higher, Push Down = lower, Push enter = correct” );

1 Like

void Start()
{
Debug.Log(“Welcome to Number Wizard. I hope you’re ready for some mile high magic.”);
//Coding from the Rocky Mountain Mile High City of Denver Colorado.
Debug.Log(“I’d like you to pick a number”);
Debug.Log("The highest number you can choose is " + max);
Debug.Log("The lowest number you can choose is " + min);
Debug.Log("Tell me if you’re number is higher or lower than " + guess);
Debug.Log(“Press the UP key if your number is higher.”);
Debug.Log(“Press the DOWN key if your number is lower.”);
Debug.Log(“Press the ENTER key if I guessed your number!”);
max = max + 1;
}

Well, I kind of modified the code and put them into strings so I can change the text better. Maybe I jumped ahead but felt like that was better.

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

public class NumberWizard : MonoBehaviour
{
    //Variables declared for Number Wizar
    int maxNumber = 1000;
    int minNumber = 1;
    int guessNumber = 500;

    //Strings needed in all scopes
    string guessText = "Tell me if your number is higher or lower than ";
    string controllerLayoutText = "Push Up = Higher, Push Down = Lower, Push Enter = Correct ";

    // Start is called before the first frame update
    void Start()
    {
        //Create string variables for easily swapping out statements.
        string welcomeText = "Namaste, welcome to my fancy ass Number Guessing Wizard yo";
        string pickNumberText = "Let's start. So for this game, you need to pick a random number";
        string maxAllowedNumberText = "The highest number you can pick is: ";
        string minAllowedNumberText = "The lowest number you can pick is: ";

        //Log to console the responses.
        Debug.Log(welcomeText);
        Debug.Log(pickNumberText);
        Debug.Log(maxAllowedNumberText + maxNumber);
        Debug.Log(minAllowedNumberText + minNumber);
        Debug.Log(guessText + guessNumber);
        Debug.Log(controllerLayoutText);

        //code to fix bug where guess does not reach 1000;
        maxNumber = maxNumber + 1;
    }

    // Update is called once per frame
    void Update()
    {
        //Detect when the up arrow key is pressed down
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            minNumber = guessNumber;
            guessNumber = (maxNumber + minNumber) / 2;
            Debug.Log("You pressed up. " + guessText + guessNumber);
            Debug.Log(controllerLayoutText);

        }

        //Detect when the down arrow key is pressed down
        else if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            maxNumber = guessNumber;
            guessNumber = (maxNumber + minNumber) / 2;
            Debug.Log("You pressed down. " + guessText + guessNumber);
            Debug.Log(controllerLayoutText);
        }

        //Detect when the return key is pressed down
        else if (Input.GetKeyDown(KeyCode.Return))
        {
            Debug.Log("Wooot, your number is " + guessNumber);
        }        
    }
}

// Yep

public class NumberWizard : MonoBehaviour
{
// declare variables for use in multiple methods
int maxNum = 1000;
int minNum = 1;
int guess = 500;
int tries = 1;

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

    Debug.Log("Hey Y'all.  Let's play Guess The Number!");
    Debug.Log("Please pick a number between " + minNum + " and  " + maxNum);
    Debug.Log("Don't tell me what it is, but remember it.");
    Debug.Log("Is your number " + guess + "?");
    Debug.Log("ArrowUp=Higher ArrowDown=Lower Enter=Correct!");
    maxNum = maxNum + 1;

}

// Update is called once per frame
void Update()
{
    //Detect when the up arrow key is pressed down
    if (Input.GetKeyDown(KeyCode.UpArrow))
    {
        Debug.Log("You pressed the Up Arrow which means my guess was too low!");
        minNum = guess;
        tries += 1;
        guess = (maxNum + minNum) / 2;
        Debug.Log("I'm changing the minimum number to " + minNum);
        Debug.Log("How about... " + guess + "?");
    }
    //Detect when the down arrow key is pressed down
    else if (Input.GetKeyDown(KeyCode.DownArrow))
    {
        Debug.Log("You pressed the Down Arrow which means my guess was too high!.");
        maxNum = guess;
        tries += 1;
        guess = (minNum + maxNum) / 2;
        Debug.Log("I'm changing the maximum number to " + maxNum);
        Debug.Log("How about... " + guess + "?");
    }
    //Detect when the Return key is pressed down
    else if (Input.GetKeyDown(KeyCode.Return))
    {
        Debug.Log("I Guessed it!!! Yay!!! And it only took " + tries + " tries!!!");
        Debug.Log(guess);
    }

}

}

image

Hello Community…!

Happy to join you all.

Hey! New to the course, and just posting what my greeting is.
I added a second script line to make said wizard a little cocky. the underlying joke being the longer it takes the wizard to guess correctly, the more of a hack the wizard looks… Seems silly, but hey its something.

   "Welcome to Number Wizard loser."
   "I am a master at guessing, I will guess any number you pick."

Well I’m just sharing what I’ve put in my Number wizard for the challenge in this lecture here.
“Salutations! Welcome to number wizard!”

Thanks for sharing!

1 Like

Hello. I wanted to add a bit of randomisation:

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

// Start is called before the first frame update
void Start()
{
    min = Random.Range(1, 100);
    max = Random.Range(min + 1, 1000);
    guess = (min + max) / 2;

    Debug.Log("Greetings. Let's play Number Wizard!");
    Debug.Log("Pick a number between " + min + " and " + max + ". Push the up arrow if my number too low, the down arrow if my number too high and Enter if my number correct.");
    Debug.Log("Is your number " + guess + "?");

    max++;
    min--;
}

// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown(KeyCode.UpArrow))
    {
        min = guess;
        guess = (min + max) / 2;

        Debug.Log("Is your number " + guess + "?");
    }

    else if (Input.GetKeyDown(KeyCode.DownArrow))
    {
        max = guess + 1;
        guess = (min + max) / 2;

        Debug.Log("Is your number " + guess + "?");
    }

    else if (Input.GetKeyDown(KeyCode.Return))
    {
        print("Yay! I got it right. Your number is " + guess);
    }
}

}

    Debug.Log("Hi yall, lets play a game.  Its called number wizard!");
    Debug.Log("In this game I want you to pick a number.  Don't tell me what it is though. I am going to try and figure out what your number is.");
    Debug.Log("The highest number it can be is: " + max);
    Debug.Log("The lowest number it can be is: " + min);
    Debug.Log("My guess is " + guess);
    Debug.Log("Tell me if your number is higher or lower than than the number I am guessing.");
    Debug.Log("Push the up arrow if your number is higher than my guess, Push the down arrow if it is lower, and push the enter button it I have guessed the correct number");
    max = max + 1;

Debug.Log(“Welcome to The Amazing Number Wizard. You’re Number Will Be Mine!”);
Debug.Log(“You Must Choose A Number And Keep It In Your Mind.”);
Debug.Log(“I Will Know Your Number Within 10 Guesses!”);
Debug.Log("The Highest Number Your Brain Can Handle Is: " +max);
Debug.Log("The Lowest Number You Can Fathom Is: " +min);
Debug.Log("Tell me if your number is higher or lower than: " +guess);
Debug.Log(“You Must Be Honest In Your Answers.”);
Debug.Log(“Push Up = Higher, Push Down = Lower, Push Enter/Return = Correct”);
max = max + 1;

1 Like

Hi all!

I’m having problems with this lecture and can’t seem to find the problem myself. After changing the variables to under class, my unity keeps giving me these syntax errors.

Here is my code:

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

public class NumberWizard : MonoBehaviour

int max = 1000;
    int min = 1;
    int guess = 500;

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

{
    

    Debug.Log("Welcome to number wizard, yo");
    Debug.Log("Welcome");
    Debug.Log("Pick a number, don't tell me what it is...");
    Debug.Log("Highest Number is: " + max);
    Debug.Log("Lowest number is: " + min);
    Debug.Log("Tell me if your number is higher or lower than my 500");
    Debug.Log("Push Up = Higher, Push Down = Lower, Push Enter = Correct");


}

// Update is called once per frame
void Update() {
    if (Input.GetKeyDown(KeyCode.UpArrow))
    {
        Debug.Log("Up Arrow key was pressed.");
        min = guess;
        Debug.Log(guess);
       
    }
    
       else if (Input.GetKeyDown(KeyCode.DownArrow))
    { 
            Debug.Log("Down Arrow key was pressed.");
    }

   else if (Input.GetKeyDown(KeyCode.Return))
    {
        Debug.Log("Return key was pressed.");
    }

}

}

Here’s my slightly groovier text…from Kiwiland:

Debug.Log(“Heya! Welcome to the Wonderful Wizard Number Game! Chur bro…!”);
Debug.Log("First I want you to choose a number… ");
Debug.Log("between " + max);
Debug.Log("and " + min);
Debug.Log("Then check if your number higher or lower than " + guess);
Debug.Log(“Then I want you to press the Up Arrow key if it’s higher, press the Down Arrow if it’s lower or press Enter if it’s correct. And keep doing it until I magically guess your number!”);
max = max + 1;

Thanks for sharing.

2 Likes

Hello.

Here is my intro from L.A. -

    Debug.Log("Hello and welcome to number wizard");
    Debug.Log("Pick a number and keep it in your head");
    Debug.Log("The wizard can't count that high so it has to be less than " + max);
    Debug.Log("The wizard is a bit of an odd one and so also doesn't know numbers less than " + min);
    Debug.Log("Press the up arrow key if your number is higher than the wizard's guess and the down arrow key if it's lower.");
    Debug.Log("Press enter if the wizard has correctly guess your number!");
    Debug.Log("The wizard's first guess is" + guess);

Privacy & Terms