Search code examples
c#unity-game-enginesyntax

Unity C# Syntax question - Transform Position


What is the reason behind this?

I'm doing the Create with code course and am at a part where I have the main camera follow the player. I got the camera to work but wanted to offset the position slightly so I thought this would work:

void Update()
{
    transform.position = player.transform.position + Vector3 (0,5, -5);
}

After Watching the video, I see that I needed to add new to the text so that it could say this.

void Update()
{
    transform.position = player.transform.position + *new* Vector3 (0,5, -5);
}

What is the reason behind this? I get that it is a new vector, but I'm trying to reason this and find out where I could learn more on proper syntax - I assume the documentation?


Solution

  • "vector3" is empty. A new assignment is required. "new vector3" is used to create vector3 struct. Because "vector3" is a struct, you need to create a new one to use.