It says object reference not set to the instance of an object 5 times, 2 leading to line 16 of
using UnityEngine;
using System.Collections;
public class OneFollower : MonoBehaviour {
private Follower HumanFollowerGet;
private Follower AlienFollowerGet;
private Follower CurrentFollower;
void Start () {
}
void Update () {
Debug.Log (CurrentFollower);
if (HumanFollowerGet.Following == true)
{
ChangeFollower (HumanFollowerGet);
}
if (AlienFollowerGet.Following == true)
{
ChangeFollower (AlienFollowerGet);
}
}
void ChangeFollower (Follower NewFollower) {
if (CurrentFollower)
{
CurrentFollower.Following = false;
}
CurrentFollower = NewFollower;
CurrentFollower.Following = true;
}
}
and 3 to this one line 46
using UnityEngine;
using System.Collections;
public class Attacking : MonoBehaviour {
// Gives the monster something to attack
private GameObject Target;
// Basic properties
public int Damage;
public int Health;
public float Speed;
// Basic distance floats
public float GeneralDistance;
public float AttackDistance;
public float GiveUpDistance;
public float AttackRange;
// Advanced distance floats
private float Distance;
private float FollowerDistance;
// Grab player
private Player PlayerScriptGet;
private GameObject PlayerGet;
private GameObject CurrentFollowerGet;
// Decides whether or not the monster can attack
private bool CanAttack;
// Sets up the state machine where the monster decides what it wants to do
private enum AttackOrGiveUp {Attack, GiveUp};
private AttackOrGiveUp AttackState;
void Start () {
Target = PlayerGet;
// Allows the monster to deal damage as soon as the game is started
CanAttack = true;
}
void Update () {
// Gives "Advanced distance floats" code
Distance = Vector2.Distance (PlayerGet.rigidbody2D.transform.position, rigidbody2D.transform.position);
FollowerDistance = Vector2.Distance (CurrentFollowerGet.rigidbody2D.transform.position, rigidbody2D.transform.position);
// If the player has no followers
if (CurrentFollowerGet == null)
{
// The monster's target will once again become the player
Target = PlayerGet;
}
// If the player is closer to the monster than the follower is
if (Distance <= FollowerDistance)
{
// The monster will target the player
Target = PlayerGet;
}
// If the follower is closer to the monster than the player is
if (FollowerDistance < Distance)
{
// The monster will target the follower instead
Target = CurrentFollowerGet;
}
// If the monster sees the player in range
if (Distance <= AttackDistance)
{
// The monster will attack the player
AttackState = AttackOrGiveUp.Attack;
}
// If the monster sees neither the player or follower
if (Distance >= GiveUpDistance)
{
// The monster will give up on attacking the player and the follower
AttackState = AttackOrGiveUp.GiveUp;
}
// If the monster is following the follower
if (AttackState == AttackOrGiveUp.Attack)
{
// If the follower's x value is greater than the sum of the monster's x value and the general distance set
if (Target.rigidbody2D.transform.position.x > rigidbody2D.transform.position.x + GeneralDistance)
{
// Move the monster to the right to catch up with the follower
rigidbody2D.transform.position += Vector3.right * Speed * Time.deltaTime;
}
// If the follower's x value is less than the difference between the monster's x value and the general distance set
if (Target.rigidbody2D.transform.position.x < rigidbody2D.transform.position.x - GeneralDistance)
{
// Move the monster to the left to catch up with the follower
rigidbody2D.transform.position += Vector3.left * Speed * Time.deltaTime;
}
// If the follower's y value is greater than the sum of the monster's y value and the general distance set
if (Target.rigidbody2D.transform.position.y > rigidbody2D.transform.position.y + GeneralDistance)
{
// Move the monster up to catch up with the follower
rigidbody2D.transform.position += Vector3.up * Speed * Time.deltaTime;
}
// If the follower's y value is less than the difference between the monstqer's y value and the general distance set
if (Target.rigidbody2D.transform.position.y < rigidbody2D.transform.position.y - GeneralDistance)
{
// Move the monster down to catch up with the follower
rigidbody2D.transform.position += Vector3.down * Speed * Time.deltaTime;
}
// If the target is in the range of an attack and the monster hasn't attacked in the past second
if (Distance <= AttackRange && CanAttack == true)
{
// Do everything mentioned in the "Attack" function
Attack ();
}
}
}
void Die () {
}
void Attack () {
// Change the current health of the target to the difference between the current health and the damage set
PlayerScriptGet.Health -= Damage;
// Start the coroutine that delays the attack
StartCoroutine (AttackDelay ());
}
IEnumerator AttackDelay () {
// The monster cannot attack
CanAttack = false;
// Wait one second
yield return new WaitForSeconds (1);
// The monster can attack again
CanAttack = true;
}
}