- Home /
Health not being subtracked.
I have the following code and it is basically my AI system fro my enemy. It controls targeting and attacking. I just added the attack() but I am get a the following error. In the health script it is trying to axcess is called ShipHealth.cs and this is where I am trying to send the information :
public void AddjustCurrentHealth(int adj)
NullReferenceException: Object reference not set to an instance of an object EnemyAi.attack () EnemyAi.cs:109)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class EnemyAi : MonoBehaviour
{
//public Transform target;//Targets.
public int rotationSpeed;//Speed enemy can rotate.
public List<Transform> targets = new List<Transform>();//List of targets.
public GameObject Gun;//Where the laser could come from.
public int Damage = -10;//This should be negative or you are going to heal it.
//public GameObject target;
private LineRenderer LR;
private float arcLength = 2.0f;
private float arcVariation = 0f;
private float inaccuracy = 0f;
private Vector3 fwd;
private Transform myTransform;//Save for the transform.
void Start()
{
LR = Gun.GetComponent<LineRenderer>();
}
void Awake()
{
myTransform = transform;//This saves our transform so we dont have to look it up all the time.
}
void Update ()
{
if(targets.Count < 1)
{
Gun.GetComponent<LineRenderer>().enabled = false;
return;//If there is no targets sets targets to 0.
}
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(targets[0].position - myTransform.position), rotationSpeed * Time.deltaTime);//Makes it look at us using the speed we set over time.
Laser();
attack();
}
void Laser()
{
Vector3 lastPoint = transform.position;
int i = 1;
LR.SetPosition(0, transform.position);//make the origin of the LR the same as the transform
while (Vector3.Distance(targets[0].transform.position, lastPoint) >.5)
{//was the last arc not touching the target?
LR.SetVertexCount(i + 1);//then we need a new vertex in our line renderer
fwd = targets[0].transform.position - lastPoint;//gives the direction to our target from the end of the last arc20.
fwd.Normalize();//makes the direction to scale
fwd = Randomize(fwd, inaccuracy);//we don't want a straight line to the target though
fwd *= Random.Range(arcLength * arcVariation, arcLength);//nature is never too uniform
fwd += lastPoint;//point + distance * direction = new point. this is where our new arc ends
LR.SetPosition(i, fwd);//this tells the line renderer where to draw to25.
i++;
lastPoint = fwd;//so we know where we are starting from for the next arc
}
}
Vector3 Randomize(Vector3 v3 , float inaccuracy2)
{
v3 += new Vector3(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f)) * inaccuracy2;
v3.Normalize();
return v3;
}
void OnTriggerEnter(Collider other) //When somthing enters the trigger.
{
if(other.tag == "Player")//Check if it was tagged player.
{
targets.Add(other.transform);//If so add it to the targets list.
Gun.GetComponent<LineRenderer>().enabled = true;
}
}
void OnTriggerExit(Collider other) //When somthing exits the trigger.
{
if(other.tag == "Player")//Check if it was tagged player.
{
targets.Remove(other.transform);//If so remove it from the targets list
}
}
private void attack()
{
print("I have made it this far");
ShipHealth sh = (ShipHealth)targets[0].GetComponent("ShipHeatlh");
sh.AddjustCurrentHealth(Damage);
}
}
Answer by clunk47 · Aug 22, 2013 at 06:28 PM
AddjustCurrentHealth has an extra 'd'. I'd imagine it's spelled correctly in the other script? Besides that, instead of:
ShipHealth sh = (ShipHealth)targets[0].GetComponent("ShipHeatlh");
sh.AddjustCurrentHealth(Damage);
Looks like you're just going for the first index of an array, so just use GetComponent like so:
targets[0].GetComponent<ShipHealth>().AdjustCurrentHealth(Damage);
Assuming AdjustCurrentHealth is spelled the same way in your ShipHealth Script.
Is there a script called LineRenderer attached to the Gun object?
Ok sorry about last question computer glitch had to restart.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Need help calling a variable from another C# script 1 Answer
Multiple Cars not working 1 Answer
Make player not be seen by AI, when player in foilage and shadows. 1 Answer
Enemy AI C# 0 Answers