NullRefrenceException: Object refrence not set to an instance of an object.
Im building a 2 player fighting game and right now I just want it to run on the same pc as a local player vs game. The damage script Im working on uses the onCollisionEnter function and deals damage, but only to one player because the objects arent refrenced. After doing some research I found that it might be because I have the same code attached to 2 different game objects but how do I avoid doing this if they have the same functions? below is the damage code: using UnityEngine; using System.Collections;
public class Damage : MonoBehaviour {
public int damage, punchDamage,kickDamage,specialDamage;
Animator anim;
public GameObject thisPlayer;
public GameObject otherPlayer;
PolygonCollider2D opponentHurtBox;
//error monitoring
public PolygonCollider2D player2Collider;
//public PolygonCollider2D thisCollider;
Health health; // refrence to health script
HitBoxManager manager;
//keybinds public string melee = "Melee_P1"; / public string kick = "Kick_P1"; public string special = "Special_P1"; /
// Use this for initialization
void Start ()
{
opponentHurtBox = otherPlayer.GetComponent<HitBoxManager>().returnCollider();
player2Collider = opponentHurtBox;
health = gameObject.GetComponent<Health> ();
manager = gameObject.GetComponent<HitBoxManager>();
anim = GetComponent<Animator> ();
}
void OnTriggerEnter2D (Collider2D other)
{
//if the tag of the collider is the same as the localcollider which is set to HurtBox in the HitBoxManager then do something....
if(other.CompareTag(opponentHurtBox.tag)==true)
{
health.TakeDamage(damage);//the player who has been entered/triggered the event must lose health
Debug.Log ("has entered the on trigger method");
}
}
}
The health code: using UnityEngine; using UnityEngine.UI; using System.Collections;
public class Health : MonoBehaviour { GameObject player; //Health public int startHealth; public int currentHealth ; public Slider healthSlider; public Collider2D body; public Rigidbody2D physicsBody; Animator anim; //refrences script Bluebeard_controller controllerScript;
bool damaged, dead;
// Use this for initialization void Start () { player = GetComponent (); anim = GetComponent (); controllerScript = GetComponent (); startHealth = (int) healthSlider.value; currentHealth = startHealth; }
// Update is called once per frame void Update () { if (damaged == true) { anim.SetBool ("Damaged", true);//<<<<<<<<<<<<<<<<<<<<<<<< no longer works } else { anim.SetBool ("Damaged", false); //reset damage } } public void addHealth(int ammount) { currentHealth += ammount; } public void TakeDamage(int ammount) { damaged = true; currentHealth -= ammount; healthSlider.value = currentHealth; //set bar to health Debug.Log ("damage was taken"); if (currentHealth <= 0 && !dead) Death (); damaged = false;
}
public void Death()
{
dead = true;
anim.SetTrigger ("Die"); //tell animator that character is dead
Destroy (body); //get rid of body collider but not body
Destroy (physicsBody);
controllerScript.enabled = false; //disable movement
}
}
And the hitBoxManager: using UnityEngine; using System.Collections;
public class HitBoxManager : MonoBehaviour { //variables for damage public int damage; public bool hurt=false; // Set these in the editor
public PolygonCollider2D frame2;
public PolygonCollider2D frame3;
public PolygonCollider2D frame4;
public PolygonCollider2D frame5;
// Used for organization
private PolygonCollider2D[] colliders;
// Collider on this game object
private PolygonCollider2D localCollider;
// We say box, but we're still using polygons.
public enum hitBoxes
{
frame2Box,
frame3Box,
frame4Box,
frame5Box,
clear // special case to remove all boxes
}
void Start()
{
// Set up an array so our script can more easily set up the hit boxes
colliders = new PolygonCollider2D[]{frame2, frame3, frame4, frame5};
// Create a polygon collider
localCollider = gameObject.AddComponent<PolygonCollider2D>();
localCollider.tag = "HurtBox";
localCollider.isTrigger = true; // Set as a trigger so it doesn't collide with our environment
localCollider.pathCount = 0; // Clear auto-generated polygons
}
void OnTriggerEnter2D (Collider2D other)
{
Debug.Log ("has entered the on trigger method");
}
public void setHitBox(hitBoxes val)
{
if(val != hitBoxes.clear)
{
localCollider.SetPath(0, colliders[(int)val].GetPath(0));
return;
}
else
localCollider.pathCount = 0;
}
public PolygonCollider2D returnCollider(){
return localCollider;
}