So after much struggle i must request help. I have 3 Scripts that pertain to the enemy and player
Player, Creature (the variables script for player and enemy) and enemy
i have an attacking system that depends on finding the gameobject the script is attached to.
the script creature is suppose to call
player.GetComponent().playerisattacking();
and
enemy.GetComponent().enemyisattacking();
even after setting the gameobjects to public and manually setting them (its intended to be multiplayer so manually setting them is not a permanent fix) i still get object reference not set to instance of an object
the gameobject in player named player and the game object named enemy is suppose to be called in creature by referring to the set variables in both of the corresponding scripts anyways heres the 3 scripts.
and yes i double checked all the gameobjects the scripts and components are set correctly
screen shot just incase you want to see
Creature script
using UnityEngine;
using System.Collections;
public abstract class Creature : MonoBehaviour
{ Enemy enemy;
Player1 player;
public float maxhealth;
public bool startattackrole;
private int d8;
public int attackstat;
public int waittime;
public int idletime;
public string name;
public float health;
public int damage;
public float range;
public bool enemyattack;
public bool Playerattack;
public int playerattackroleoutput;
public int enemyattackroleoutput;
public bool playergo;
public bool enemygo;
//1 attack per seond
public float attackSpeed = 2f;
public Animator attackanimaton;
public void GetHit(int playerDamage)
{
health = health - playerDamage;
}
protected abstract IEnumerator Autoaction();
void attackisgo()
{
if (playerattackroleoutput > enemyattackroleoutput)
{
player.GetComponent<Player1>().playerisattacking();
}
if (enemyattackroleoutput > playerattackroleoutput)
{
enemy.GetComponent<Enemy>().enemyisattacking();
}
}
public void attackrolego()
{
Debug.Log("Attackrolego has activated");
d8output();
enemyattackroleoutputgo();
d8output();
playerattackroleoutputgo();
attackisgo();
}
void enemyattackroleoutputgo()
{
enemyattackroleoutput = d8 + attackstat;
}
void playerattackroleoutputgo()
{
playerattackroleoutput = d8 + attackstat;
}
void d8output()
{
d8 = Random.Range(1, 8);
}
// Use this for initialization
void Awake() {
attackanimaton = GetComponent<Animator>();
Playerattack = false;
enemyattack = false;
d8 = Random.Range(1, 8);
playergo = false;
enemygo = false;
startattackrole = false;
}
// Update is called once per frame
void Update() {
}
}
Player script
using UnityEngine;
using System.Collections;
using System;
public class Player1 : Creature
{
bool activeattack;
public static bool isAttacking;
public static bool playerattack;
public float fpstargetdistance;
public Transform fpstarget;
GameObject player;
void targetingsystem()
{
fpstargetdistance = Vector3.Distance(fpstarget.position, transform.position);
}
// Use this for initialization
void Awake()
{
health = maxhealth;
Vector3 player = transform.position;
isAttacking = false;
}
// Update is called once per frame
void Update()
{
targetingsystem();
attacktrue();
}
protected override IEnumerator Autoaction()
{
if (playerattack == true && (fpstargetdistance < range))
{
Debug.Log("player is attacking");
playergo = false;
enemyattack = false;
attackanimaton.SetBool("isAttacking", true);
attackanimaton.SetFloat("Attack", 9.0f);
fpstarget.GetComponent<Enemy>().GetHit(damage);
yield return new WaitForSecondsRealtime(idletime);
attackanimaton.SetFloat("Attack", 4.0f);
Debug.Log("player waiting");
yield return new WaitForSecondsRealtime(waittime);
enemyattack = true;
}
}
public void playerisattacking()
{
if (playergo == true)
Debug.Log("player is attacking frist");
playerattack = true;
StartCoroutine(Autoaction());
}
void attacktrue()
{
if (Input.GetMouseButtonDown(1))
{
attackrolego();
}
}
}
Enemy script
using UnityEngine;
using System.Collections;
public class Enemy : Creature {
NavMeshAgent navAgent;
public GameObject enemy;
bool activeattack;
public float chasingRange;
public static bool block;
public float chasingrange;
public float fpstargetdistance;
public float enemylookdistance;
public float attackdistance;
public float enemymovementspeed;
public float damping;
public Transform fpstarget;
Rigidbody therigidbody;
Renderer myrender;
// Use this for initialization
void targetingsystem()
{
fpstargetdistance = Vector3.Distance(fpstarget.position, transform.position);
}
void Awake () {
Vector3 enemy = transform.position;
navAgent = GetComponent<NavMeshAgent>();
myrender = GetComponent<Renderer>();
therigidbody = GetComponent <Rigidbody>();
}
// Update is called once per frame
void Update () {
targetingsystem();
}
protected override IEnumerator Autoaction()
{
if (enemyattack == true && (fpstargetdistance < range))
{
enemygo = false;
Debug.Log("enemy is attacking");
Playerattack = false;
attackanimaton.SetBool("isAttacking", true);
attackanimaton.SetFloat("Attack", 9.0f);
fpstarget.GetComponent<Player1>().GetHit(damage);
yield return new WaitForSecondsRealtime(idletime);
attackanimaton.SetFloat("Attack", 4.0f);
Debug.Log("waiting");
yield return new WaitForSecondsRealtime(waittime);
Playerattack = true;
}
}
public void enemyisattacking()
{
if (enemygo == true)
Debug.Log("enemy is attcking first");
enemyattack = true;
StartCoroutine(Autoaction());
}
}