Operator '<' cannot be used with a left hand side of type 'Ob

Hi, I’m new on Unity and I want to create a game but I have a problem and I’m stuck.
The console says : Assets/AI/ZombieAI.js(29,16): BCE0051: Operator ‘<’ cannot be used with a left hand side of type ‘Object’ and a right hand side of type ‘int’.
This is my script :

#pragma strict

var Distance;
var Target : Transform;
var LookAtDistance = 30;
var chaseRange = 15;
var attackRange = 2.2;
var moveSpeed = 3;
var Damping = 5;
var attackRepeatTime = 0.5;

var TheDammage = 5;

private var attackTime : float;

var controller : CharacterController;
var gravity : float = 20;

private var MoveDirection : Vector3 = Vector3.zero;

function Start () {
attackTime = Time.time;
FindHealth();
}

function Update () {
Distance = Vector3.Distance(Target.position, transform.position);

if(Distance < LookAtDistance){
lookAt();
}

if(Distance < attackRange){
attack();
}

else if(Distance < chaseRange){
chase();
}
}

function lookAt(){
var rotation = Quaternion.lookRotation(Target.position - transform.position);
trasform.rotation = Quaternion.Slerp(transform.rotation, rotation,Time.deltaTime * Dampling);
}

function chase(){
GetComponent.().Play(“walk”);

moveDirection = transform.forward;
moveDirection *= moveSpeed;

moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}

function attack(){
if(Time.time > attackTime){
GetComponent.().Play(“attack”);

Target.SendMessage(“ApplyDammage”, TheDammage);
Debug.log(“The ennemy has attacked”);
attackTime = Time.time + attackRepeatTime;
}
}

function ApplyDammage(){
chaseRange += 35;
moveSpeed += 2;
LookAtDistance += 45;
}

function FindHealth(){
Target = GameObject.Find(“PlayerSats”).GetComponent(PlayerStats).transform;
}

The line affected is : if(Distance < LookAtDistance){

Thank’s to help me!

Firstly, use code tags

Secondly: Specify the type of Distance, the compiler thinks it’s an Object where as you think it’s a float.

var Distance : float;

thank you :smile:

1 Like

When using UnityScript it’s better to give a type to everything, stops a lot of confusing errors occurring later

1 Like

This. UnityScript pretends to allow dynamic typing, but it doesn’t really. If the compiler can’t make a good guess at the type it just makes it an object and moves on.

C# doesn’t have such nonsense.

1 Like