Vector3.magnitude

How can I use vector3.magnitude function inside my script, how can I print the value of the magnitude in the console?
Can someone give me an example?

I read the documentation on vector3.magnitude, but it’s not very helpful, I don’t understand how to use it…
I attached a script to a cube and inside the start function, I wrote:
Debug.Log(vector3.magnitude);

When I do this, I get an error that says:
An object reference is required to access non-static member `UnityEngine.Vector3.magnitude’

Please help me, I am a beginner and don’t know much, any help will be most appreciated!

When you type ‘Vector3.magnitude’, you’re essentially trying to get the magnitude of the class itself, which is basically what ‘static’ means here. If we use a car analogy, you’re asking, “What color is a Toyota Camry?” That question is impossible to answer, because Toyota Camry’s can come in any color.

What you need to do is ask “What color is that Toyota Camry?” which is a question you can answer. That’s what it means by “object reference”. The simplest object reference of a Vector3 you can use is the current object’s position:

void Update() {
Debug.Log(transform.position.magnitude);
}

transform.position is an instance of Vector3, and therefore, has a magnitude. If you put that line on your script and move that object around, you’ll be able to see the magnitude change.

1 Like

Thank you very much, now I get what it meant by “object reference”, I followed your advice and now there are no errors, thanks a lot!