Be the first to post for 'Updating Shared Variables'!

If you’re reading this, there probably aren’t very many posts yet. But don’t worry, you can be the first! Either create a new post or just reply to this one to say ‘hi’.

Here is the code I wrote while I had paused the video. It seems to be working properly. :slight_smile: Excited to have gotten it to work on my own.

public class ConsolePrint : MonoBehaviour {

public Vector2 location;
public Vector2 homeLocation;
public Vector2 pathToHome;

// Use this for initialization
void Start () {
   
    print("I always wondered what it was like out there...");
	print("On the other side of that invisible barrier");
	print("It is pretty great out here.");
	print("But it can be really scary");
	print("There are loud monsters that can run really fast");
	print("It is getting dark and cold now");
	print("I want to go home and eat my num nums");
	print("But I wandered so far that I'm not sure of my way back");
	print("The sooner I get home, the sooner I can take a cat nap");
    if (location == homeLocation)
    {
        print("You are home!");
    }
    else
    {
        print("Path to Home:" + pathToHome);
        print("Distance to Home:" + pathToHome.magnitude);
    }

}

// Update is called once per frame
void Update () {

    
    if (Input.GetKeyDown(KeyCode.LeftArrow))
    {
        location = location + new Vector2(-1.0f, 0);
        print("Location: " + location);
        print("Left key pressed");
        pathToHome = homeLocation - location;
        print("Distance to Home:" + pathToHome.magnitude);
    }

    if (Input.GetKeyDown(KeyCode.RightArrow))
    {
        location = location + new Vector2(+1.0f, 0);
        print("Location: " + location);
        print("Right key pressed");
        pathToHome = homeLocation - location;
        print("Distance to Home:" + pathToHome.magnitude);
    }

    if (Input.GetKeyDown(KeyCode.UpArrow))
    {
        location = location + new Vector2(0, +1.0f);
        print("Location: " + location);
        print("Up key pressed");
        pathToHome = homeLocation - location;
        print("Distance to Home:" + pathToHome.magnitude);
    }

    if (Input.GetKeyDown(KeyCode.DownArrow))
    {
        location = location + new Vector2(0, -1.0f);
        print("Location: " + location);
        print("Down key pressed");
        pathToHome = homeLocation - location;
        print("Distance to Home:" + pathToHome.magnitude);
    }
}

}

Well done!

1 Like

Hi Ben,
Everything is working great with the code except for the life of me I cannot figure out why everytime I input a key entry it keeps telling me “I am home”

public class ConsolePrinter : MonoBehaviour {

    public Vector2 location;
    public Vector2 homeLocation;
    
    // Use this for initialization
    void Start() {
            print("Welcome to Go Home!");
            print("A game where you need to find your way back.");
    }

    // Update is called once per frame
    void Update() {
            if (Input.GetKeyDown(KeyCode.LeftArrow))
            {
                    location = location + new Vector2(-1, 0);
                    Vector2 pathToHome = homeLocation - location;
                    print("Distance to home: " + pathToHome.magnitude);
                    if (location == homeLocation);
                    {
                            print("I am home!");
                    }
            }
            if (Input.GetKeyDown(KeyCode.RightArrow))
            {
                    location = location + new Vector2(1, 0);
                    Vector2 pathToHome = homeLocation - location;
                    print("Distance to home: " + pathToHome.magnitude);
                    if (location == homeLocation);
                    {
                            print("I am home!");
                    }
            }
            if (Input.GetKeyDown(KeyCode.UpArrow))
            {
                    location = location + new Vector2(0, 1);
                    Vector2 pathToHome = homeLocation - location;
                    print("Distance to home: " + pathToHome.magnitude);
                    if (location == homeLocation);
                    {
                            print("I am home!");
                    }
            }
            if (Input.GetKeyDown(KeyCode.DownArrow))
            {
                    location = location + new Vector2(0, -1);
                    Vector2 pathToHome = homeLocation - location;
                    print("Distance to home: " + pathToHome.magnitude);
                    if (location == homeLocation);
                    {
                            print("I am home!");
                    }
            }
    }

}

hi!
i’m having trouble seeing the X and Y on the console print script in the inspector when pressing the play button.

Privacy & Terms