Be the first to post for 'Declaring Your Own Classes'!

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’.

public class ConsolePrint : MonoBehaviour {

Vector2 location;
Vector2 homeLocation;
Vector2 pathToHome;

// Use this for initialization
void Start () {
   
    Vector2 homeLocation = new Vector2(2.1f, 2.1f);
    Vector2 pathToHome = homeLocation - location;

}

We can initialize all of the needed variables for the void start () section to access.

Hi Ben,

I used the exact code that you showed during the lecture, but the moment I run the same in Unity the values in the console keep updating even without me pressing any keys. Why is that ? The code is used is given below :-

public class ConsolePrinter : MonoBehaviour {

Vector2 location;
Vector2 homeLocation;

// Use this for initialization
void Start () {
    
    Vector2 pathToHome = homeLocation - location;
    print("Welcome to go home");
    print("A game where you need to find your way back");
    print("path To Home:" + pathToHome);
    print("Diatnace to home: " + pathToHome.magnitude);
    
    if(location == homeLocation)
    {
        print("Destination reached");
    }

}

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

    print("Location" + location);
   if(Input.GetKeyDown(KeyCode.LeftArrow))
    {
        print("Left key is pressed");
    }
   if(Input.GetKeyDown(KeyCode.RightArrow))
    {
        location = new Vector2(2, 3);
        print("Right Key is pressed");
    }

}

}

Regards,
Srini

I would choose Google Cardboard.

Privacy & Terms