- Home /
cant assign variables value
i have a ai script but i cannot assign the target variable. i dont know whats wrong could someone tell me please? here is the code
var IsPlayingOtherAnim = false;
var Distance;
var Player : GameObject;
var lookAtDistance = 25.0;
var ChaseRange = 15.0;
var attackRange = 1.5;
var moveSpeed = 5.0;
var Damping = 6.0;
var AttackRepeatTime = 1;
static var TheDamage = 10;
private var attackTime : float;
var controller : CharacterController;
var Gravity : float = 20;
private var MoveDirection : Vector3 = Vector3.zero;
function start ()
{
attackTime = Time.time;
Player = GameObject.Find("Player");
}
function Update ()
{
Distance = Vector3.Distance(Player.position, transform.position);
if (Distance < lookAtDistance)
{
lookAt();
}
if (Distance > lookAtDistance)
{
renderer.material.color = Color.green;
}
if (Distance < attackRange)
{
Attack();
}
else if (Distance < ChaseRange)
{
Chase ();
}
}
function lookAt ()
{
renderer.material.color = Color.yellow;
var rotation = Quaternion.LookRotation(Player.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
function Chase ()
{
renderer.material.color = Color.red;
moveDirection = transform.forward;
moveDirection *= moveSpeed;
moveDirection.y -= Gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
function Attack ()
{
if (Time.time > attackTime)
{
Player.SendMessage("ApplyDammage", TheDamage, SendMessageOptions.DontRequireReceiver);
Debug.Log ("the enemy has attacked");
attackTime = Time.time + AttackRepeatTime;
}
}
function ApplyDamage ()
{
ChaseRange = 30;
moveSpeed = 4;
lookAtDistance = 40;
}
$$anonymous$$y javascript is poor but is it looking at your player but not moving towards?
Does the enemy have a rigidbody?
If both of those are true try putting this in the Chase() function
rigidbody.velocity = transform.forward * 10;
And see if it starts moving.
well i fixed that issue the Player var was originally a transform using a character controller ins$$anonymous$$d of a rigidbody using gameobject so i changed GameObject.Find("Name")
to
GameObject.Find("Name").transform
Answer by ramonfr · Dec 24, 2014 at 08:13 PM
Did you notice that the function "Start" is called "start"? Unity will not read the Start function because of the low case letter. And it's in that function where the variable Player is assigned to a value.
Answer by George_1999 · Dec 24, 2014 at 11:37 AM
Make sure the player in your scene is in fact named "Player"
it is Named "Player" it is only giving me an error that says Object reference not set to an instance of an object
Find should find it then, does it give a line number for that error and also can you run the game without "maximize on play" and upload a screen shot. Just in case we can spot something.
it is giving me that error only when i run the scene. because the Player variable is not assigned and the script relies on it to work properly. here is the full error i get when i run the scene.
NullReferenceException: Object reference not set to an instance of an object ZombieAI.Update () (at Assets/$$anonymous$$yScripts/ZombieAI.js:33)