Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by ricotomo · Feb 12, 2016 at 05:54 PM · scripting problemerrorgameobjectnullreferenceexceptionnull

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;
 }

} @GameScience @aldonaletto @jabez

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Help Me on Storing GameObject!!! 1 Answer

Space Shooter Score Text NullReference error 3 Answers

Why am I getting a null reference error? 1 Answer

Null Reference in UnityStandardAssets.Utility.WaypointProgressTracker.Update 0 Answers

Bugs on my C# Scripts? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges