• 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 /
avatar image
Question by RomanorumLegio · Jun 16, 2013 at 08:05 PM · c#buggetcomponentnullreferenceexeption

Object Reference not Set to an Instance of an Object in C# (Closed)

I have a script detecting if an object has collided with another. I've done some tests by calling other variables between the two scripts (now deleted) and that worked fine, no errors. When tiring to set one variable in the script Stats equal to the called variable from the script RocketDamage I get a NullReferenceExeption.

Stats script (error is on the line "isRocketHit = rocketDamage.rocketHit;")

 using UnityEngine;
 using System.Collections;
 
 public class Stats : MonoBehaviour {
     public int playerControl = 1;
     public float hp = 1002f;
     public float armor = 1005f;
     RocketDamage rocketDamage;
     public float rocketOut;
     float isRocketHit;
     
 
     // Use t$$anonymous$$s for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         
         //call RocketDamage script because OnTriggerEnter is only allowed once per script
         rocketDamage = GetComponent<RocketDamage>();
         isRocketHit = rocketDamage.rocketHit;
         
         //take dame in returned that $$anonymous$$t is true
         if(isRocketHit == 1){
             hp -= Mathf.Round(100 + (100/(armor+1)));
             armor -= 200;
             //output that damage from rocket has been taken
             rocketOut = 0;
         }
     }
     
     //take damage if $$anonymous$$t by bullet
     void OnTriggerEnter(Collider bullet_basic){
         hp -= Mathf.Round(1 + (1000/(armor+1)));
         if(armor > 0){
             armor -= 5;
         }
     }
 }

RocketDamage script:

 using UnityEngine;
 using System.Collections;
 
 public class RocketDamage : MonoBehaviour {
     
     Stats stats;
     public float rocketHit = 0f;
 
     // Use t$$anonymous$$s for initialization
     void Start () {
         
         //call Stats script
         stats = GetComponent<Stats>();
         
     }
     
     // Update is called once per frame
     void Update () {
         //detect if damage from rocket has been taken
         if(stats.rocketOut == 0){
             rocketHit = 0;
         }
     }
     //detect rocket explosion
     void OnTriggerEnter(Collider explosion_basic){
         rocketHit = 1;
     }
 }


Thanks in advance, I been attempting to debug and Google search answers for hours now.

Comment

People who like this

0 Show 0
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
Best Answer

Answer by Slobdell · Jun 16, 2013 at 08:24 PM

What is your RocketDamage component attached to? It's not finding your component because it doesn't know w$$anonymous$$ch object to get it from. I'm not sure about your syntax either, may or may not work, but when I get a reference to a script it's a little different. Try changing

 RocketDamage rocketDamage = GetComponent<RocketDamage>;

to

 RocketDamage rocketDamage = (RocketDamage)t$$anonymous$$s.GetComponent(typeof(RocketDamage));

Or if the script is attached to somet$$anonymous$$ng else, just change "t$$anonymous$$s" to whatever GameObject the script is attached to. Same t$$anonymous$$ng for getting the stats script.

Steve

Comment

People who like this

0 Show 3 · 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
avatar image RomanorumLegio · Jun 16, 2013 at 08:39 PM 0
Share

The script is part of the same object, yes. I tried changing it to what you had, but it still gives the same error. The error is on the following line.

avatar image Slobdell · Jun 16, 2013 at 08:43 PM 0
Share

Did you forget to actually attach the script to the object? Or trying to get it from the wrong object?

avatar image RomanorumLegio · Jun 26, 2013 at 03:41 AM 0
Share

I solved this by combining it with the original script, but I had a similar problem with another script. I found my solution with that one. I needed to make a GameObject variavle and set it with GameObject.FindGameObjectWithTag. That solve my problem.

Thanks for your help, Steve.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to get a variable value from another script(C#)? 1 Answer

insert script question 1 Answer

Using GetComponent in multiple scripts for same component? C# 2 Answers

C# about NGUITools.AddChild and GetComponent Null 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