I am trying to reference a script called “FighterArm” via a script called “UnitController”.
However, it keeps giving that error.
The Unit Controller code is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class UnitController : MonoBehaviour {
//Fetching Variables
private NavMeshAgent navAgent;
private Transform currentTarget;
public UnitStats unitStats;
private float attackTimer;
public bool FAA;
public FighterArm fighterArm;
//Scripting Starts
public void Start()
{
navAgent = GetComponent<NavMeshAgent>();
attackTimer = unitStats.attackSpeed;
FAA = false;
fighterArm = gameObject.GetComponent<FighterArm>();
}
private void Update()
{
attackTimer += Time.deltaTime;
if(currentTarget != null)
{
navAgent.destination = currentTarget.position;
var distance = (transform.position - currentTarget.position).magnitude;
if(distance <= unitStats.attackRange)
{
Attack();
Debug.Log("GoingToAttack");
}
}
}
public void MoveUnit(Vector3 dest)
{
currentTarget = null;
navAgent.destination = dest;
}
public void SetSelected(bool isSelected)
{
transform.Find("Highlight").gameObject.SetActive(isSelected);
}
public void SetNewTarget(Transform enemy)
{
currentTarget = enemy;
Debug.Log("MovingToEnemy");
}
public void Attack()
{
if(attackTimer >= unitStats.attackSpeed)
{
//RTSManager.UnitTakeDamage(this, currentTarget.GetComponent<UnitController>());
//attackTimer = 0;
fighterArm.FAanimation = true;
Debug.Log("Attacking");
}
}
public void TakeDamage(UnitController enemy, float damage)
{
StartCoroutine(Flasher(GetComponent<Renderer>().material.color));
}
IEnumerator Flasher(Color defaultColor)
{
var renderer = GetComponent<Renderer>();
for (int i = 0; i < 2; i++)
{
renderer.material.color = Color.gray;
yield return new WaitForSeconds(.05f);
renderer.material.color = defaultColor;
yield return new WaitForSeconds(.05f);
}
}
}
The FighterArm Code is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FighterArm : MonoBehaviour
{
public Animator animator;
public bool FAanimation;
// Start is called before the first frame update
void Start()
{
animator = gameObject.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (FAanimation == true)
{
animator.SetBool("Attacking", true);
Debug.Log("Receiving Attack");
}
// if (FAanimation == false)
//{
// animator.SetBool("Attacking", false);
// }
}
}
The full error message is:
[ICODE]
NullReferenceException: Object reference not set to an instance of an object
UnitController.Attack () (at Assets/Scripts/UnitController.cs:74)
UnitController.Update () (at Assets/Scripts/UnitController.cs:45)
[/ICODE]
Some good information:
-
UnitController is not on an object. But another script called PlayerUnitController is, and all that does is reference UnitController.
-
The object that the FighterArm script is on, is parented under the original object that PlayerUnitController is on.
Context:
All I want is when you click on the playerobject called “Fighter” and click on an enemy. He flies up and starts the attacking animation. The animation is only done on another mesh parented under the Fighter object and is not on the fighter object itself.