- Home /
Question by
Mattendo · Mar 15, 2013 at 03:53 AM ·
errorvector3declaration
C# Vector3 error: Expression denotes a `type'...
I read that you can't use var in c# so you have to use new Vector3, however I'm having some problems
class PlatformerControllerMovement{
//The speed when walking.
public float walkSpeed = 3.0f;
//The current move direction in x-y. This will always been (1,0,0) or (-1,0,0)
[System.NonSerialized]
public Vector3 direction = new Vector3(0,0,0); **//error on this line**
//The current movement speed.
[System.NonSerialized]
public float speed = 0.0f;
//Is the user pressing the left or right movement keys?
[System.NonSerialized]
public bool isMoving = false;
}
PlatformerControllerMovement movement = new PlatformerControllerMovement();
void UpdateSmoothedMovementDirection(){
var h = Input.GetAxisRaw("Horizontal");
movement.isMoving = Vector3 (h,0,0);
if(movement.isMoving){
movement.direction = Vector3 (h,0,0);
}
}
I'm not sure what I'm doing wrong. Thanks in advance for any help
Comment
Best Answer
Answer by robertbu · Mar 15, 2013 at 04:09 AM
Most of the type this error happens when you forget to put 'new' in front of a Vector3. I see that problem on lines 23 and 26 in the code above. There is a bracketing problem. Line 16 needs to be moved to the end of the file. And Line 23 does not make sense. Here is the file with all the fixes but the last one (I have no idea what to do to fix that one).
using UnityEngine;
using System.Collections;
class PlatformerControllerMovement {
//The speed when walking.
public float walkSpeed = 3.0f;
//The current move direction in x-y. This will always been (1,0,0) or (-1,0,0)
[System.NonSerialized]
public Vector3 direction = new Vector3(0,0,0); //error on this line**
//The current movement speed.
[System.NonSerialized]
public float speed = 0.0f;
//Is the user pressing the left or right movement keys?
[System.NonSerialized]
public bool isMoving = false;
PlatformerControllerMovement movement = new PlatformerControllerMovement();
void UpdateSmoothedMovementDirection(){
var h = Input.GetAxisRaw("Horizontal");
// This line doesn't make sense;
movement.isMoving = new Vector3 (h,0,0);
if(movement.isMoving){
movement.direction = new Vector3 (h,0,0);
}
}
}
That fix'd it. The error on line 23 was a my fault. Was supposed to be movement.is$$anonymous$$oving = $$anonymous$$athf.Abs (h) > 0.1;
Thanks alot!
Your answer
![](https://koobas.hobune.stream/wayback/20220613094439im_/https://answers.unity.com/themes/thub/images/avi.jpg)