- Home /
My Vector3 changes all values to 0
Hi guys. Im just playing around in Unity and noticed my code for my Vector3 doesnt work and it doesnt make sense or im just stupid ?
var vectorPosition : Vector3 = Vector3(10, 50, 10);
function Update () {
if(Input.GetButtonDown("Jump")) {
transform.position = vectorPosition;
}
}
Every time a press space it just transforms it to 0, 0, 0. Help please? Thanks
Answer by Dave-Carlile · Jan 28, 2013 at 07:55 PM
It's generally not a good idea to initialize variables values during variable declaration. This is because public variables show up in the inspector, so any value there overrides the initialized value and can cause issues and confusion.
If you look in the inspector I expect you'll see the vectorPosition value is <0,0,0>. Try changing the value in the inspector to <10,50,10> and I suspect it will work.
It's usually better to initialize the value in the Awake or Start functions.
var vectorPosition : Vector3;
function Start()
{
vectorPosition = Vector3(10, 50, 10);
}
Good answer. The only thing I'd add to that is that, if the variable doesn't need to be read anywhere else, and you don't want the variable to show up in the inspector, you could always make it a private variable (private var vectorPosition: ...).
If you need it to be public, but not show up in the inspector, use HideInInspector.
Thanks so much guys ! Sorry just getting use to everything in Unity.
No need to apologize! We had to learn it at one point too.
Your answer
Follow this Question
Related Questions
Use yAxis as a Vector3(0,0,yAxis) paramenter? 3 Answers
Relative Rotation 1 Answer
Need some help with to grap .x with a Raycasthit (c#) 1 Answer
Distance won't work 1 Answer