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 christopherking98 · Aug 11, 2017 at 06:49 AM · null reference exception

can someone help me with my code error

I am just starting out making a game and all is going well exept for my defense of the player for some reason. it shows up as a

NullReferenceException: Object reference not set to an instance of an object hurtplayer.OnCollisionEnter2D (UnityEngine.Collision2D other) (at Assets/script/hurtplayer.cs:20)

can someone please help me find out were I cant seam to find the problem.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class playerstats : MonoBehaviour {
     public int currentlvl;
     public int exp;
     public int[] tolvlup;
     public int[] hplevels;
     public int[] attacklevels;
     public int[] defenselevels;
     public int chealth;
     public int cattack;
     public int cdeffense;
     private playerhealth playerhealth1;
 
 
 
     void Start () {
         chealth = hplevels [1];
         cattack = attacklevels [1];
         cdeffense = defenselevels [1];
         playerhealth1 = FindObjectOfType <playerhealth> ();
 
 
     }
     
 
     void Update () {
         if (exp >= tolvlup [currentlvl]) {
             //currentlvl++;
             levelup ();
 
         }
 
 
     }
     public void addexp(int expadd){
         exp += expadd;
 
     }
     public void levelup (){
         currentlvl++;
         chealth = hplevels [currentlvl];
         playerhealth1.playermhealth = chealth;
         playerhealth1.playerchealth += chealth - hplevels[currentlvl - 1];
         cattack = attacklevels [currentlvl];
         cdeffense = defenselevels [currentlvl];
 
 
 
     }
 
 }
 

 using UnityEngine;
 using System.Collections;
 public class hurtplayer : MonoBehaviour{
     
 
     public int damagetogive;
     private int currentdamage;
     private playerstats ps;
 
 
     void start(){
         ps = FindObjectOfType<playerstats> ();
 
     }
     void update(){
 
     }
     void OnCollisionEnter2D(Collision2D other){
         if (other.gameObject.tag == "Player") {
             
             currentdamage = damagetogive - ps.cdeffense;
             //currentdamage = damagetogive;           
 
 
             other.gameObject.GetComponent<playerhealth> ().hurtplayer (currentdamage);
         }
 
 
     }
 
 
 }

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class playerhealth : MonoBehaviour {
     public int playermhealth;
     public int playerchealth;
 
     // Use this for initialization
     void Start () {
         playerchealth = playermhealth;
     }
     
     // Update is called once per frame
     void Update () {
         if (playerchealth < 0) {
             gameObject.SetActive (false);
 
         }
 
 
 
 
         if (playerchealth > playermhealth) {
             playerchealth = playermhealth;
         }
 
 
 
 
         
     }
     public void hurtplayer(int currentdamage)
     {
 
         playerchealth -= currentdamage;
     }
 
     public void setmaxhealth()
     {
         playermhealth = playerchealth;
     }
 
 
 }
 

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by UltraGearGames · Jan 25, 2019 at 09:28 PM

one of the gameobjects doesnt have a trigger on it. Simple stuff. Also dont forget to add a rigidbody (or rigidbody2D) to at least one of the 2 objects!

Comment
Add comment · Share
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

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

111 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help with NullReferenceException 0 Answers

Not sure what this error is telling/asking me to do? 1 Answer

Null Reference Error 0 Answers

NullReferenceException... but it was just set...? 1 Answer

Cannot access 2D array element 1 Answer


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