• 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
0
Question by MezFo · Oct 21, 2017 at 02:22 AM · c#unity 5script.nullreferenceexceptiongetcomponent

Has my project's data corrupted? Script keeps returning null but 20 minutes ago it wasn't?

Hi,

I am working on a part of my project which makes a prefab instantiate in the character's hand when I drop it in a hotkey/slot. The script on my slot checks the tag of the image i have dropped in the slot and then tells the script on my character's hand which weapon it is so it knows which gun prefab to instantiate. My first attempt I was getting the exact same problem (return null when trying to contact script) but I realized it was because I had another hand model in my scene that had the same name as the hand of my character but no script. I changed the name of my chracter's hand and it started working perfectly. I saved my scene and project and went to play R6s for 20-30 mins. Came back to my PC and started looking through my gun prefabs in resources trying to decide what gun to put in the scene for scaling and stuff. For some reason Unity crashed when I clicked on one of the prefabs and after I rebooted Unity I am now getting the exact same problem but there is no gameobjects in the scene with the same names or anything else I can find that is causing the issue.

 public GameObject PM40;
 
 public void Equip () {
                //Trying to get the script on hand (Hold_Rr)
         WeaponChange weaponchange = GameObject.Find ("Hold_Rr").GetComponent<WeaponChange> ();
                 // Check the tag of image
         if (SlotItem.gameObject.tag == "PM40") {
             Debug.Log ("Tag PM40");
                         //Check if script is null
             if (weaponchange == null) {
                 Debug.Log ("null");
                 return;
             }
                         //Change GameObject to prefab of PM40 pistol
             weaponchange.Holder1 = PM40;
                         //run the weapon change function to instantiate it
             weaponchange.PistolHolder ();
 
 
         }
 
     }

This is the script attached to the hotkey. I checked to make sure it was finding the Hold_Rr object (hand) and it definitely is.

 public GameObject Holder1;
 public void PistolHolder () {
         
         Debug.Log ("PistolHolder()");
 
         GameObject gun = (GameObject)Instantiate(Holder1, transform.position, transform.rotation);
         gun.transform.parent = pistolHolder.transform;
         gun.transform.localRotation = Quaternion.identity;
         gun.transform.localPosition = new Vector3 (0, 0, 0);
 
     }

This is the function in the script on the Hold_Rr object that the other script is supposed to call after updating the Holder1 variable.

Something I noticed was the Hold_Rr object's name was in bold letters in the inspector and the Layer dropdown in bold letters as well. I reverted it and now it looks fine.

Please help me.. =(

Comment
Add comment · Show 6
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 unit_nick · Oct 21, 2017 at 04:04 AM 0
Share

I'm kinda having trouble following the script. I believe many problems can be solved yourself simply by using more descriptive names. I mean what is a pistol holder, is it a hand? And Holder1 is assigned a pistol model, why is it not called pistol$$anonymous$$odel? And you have a game object called "Hold_Rr", why is this not called rightHand or similar? I also think you should steer well clear of using function names that are the same as class/object names which can be seen on lines 2 & 7 of the hotkey script where you have a PistolHolder function and a pistolHolder instance.

avatar image MezFo unit_nick · Oct 21, 2017 at 04:50 AM 0
Share

Isn't it only bad to have functions and instances with the exact same names? In my script the function has a capital p and instance is lowercase. pistolHolder instance is from public GameObject pistolHolder; in the same script and is an empty object in the hand (Hold_Rr). I know the names are confusing I was going to start editing that but i'm busy trying to solve this first and it definitely is not causing any of the problems.

pistol holder is the empty object that is a child of the hand (Which is "Hold_Rr"). So in the Hold_Rr (Hand) there would be multiple empty objects for the different types of weaponds like pistolholder then primaryholder then meleeholder etc. It would be better to name pistolholder secondaryholder but the secondary is always going to be a pistol where as the primary could be shotguns, rifles etc. Everything in the PistolHolder function works fine though when it is called from the hotkey/slot script (The top script in my post) using weaponchange.PistolHolder();. The Holder1 is an empty GameObject in the WeaponChange script (bottom script) which is what will be instantiated in the emptyobject called pistolholder which is a child of "Hold_Rr" (the hand model) that has the WeaponChange script. It is just the part where the top script needs to tell the bottom script to make Holder1(which starts as empty) = the pistol prefab which is P$$anonymous$$40.

I tried this -

 public WeaponChange weaponchange;
 
 public void Equip () {
         if (SlotItem.gameObject.tag == "P$$anonymous$$40") {
             Debug.Log ("Tag P$$anonymous$$40");
             if (weaponchange == null) {
                 Debug.Log ("null");
                 return;
             }
             weaponchange.Holder1 = P$$anonymous$$40;
             weaponchange.PistolHolder ();
 
 
         }
 
     }

This way it worked because I made the WeaponChange public and just added it in the inspector so the script already knows what it is. This means for some reason it is not able to find the script component when trying to do it in the script and I don't understand why. Can you see anything wrong with just this part (Below) because that is what's returning null even though it should be able to find the WeaponChange script in the GameObject "Hold_Rr".

 WeaponChange weaponchange = GameObject.Find ("Hold_Rr").GetComponent<WeaponChange> ();
 
 weaponchange.Holder1 = P$$anonymous$$40;

Thanks.

avatar image unit_nick MezFo · Oct 21, 2017 at 05:07 AM 0
Share

Well without using a slot assigning P$$anonymous$$40 to Holder1 is assigning a null isn't it? You need to find and instantiate the prefab.

Show more comments
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by MezFo · Oct 21, 2017 at 06:33 AM

It must have been something to do with Unity crashing even though I saved. I just changed the name of the hand model from Hold_Rr to Hold_Rrr and changed it in the Find() function and now it works again. Changed it back to Hold_Rr and it still works. I'm guessing even though it said the hand was called Hold_Rr in the scene and inspector, Unity thought it was still "Hold_R" which is what it was called before I solved it the first time before it crashed... even though I saved.

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.

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

433 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 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 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 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

Using a variable value with GetComponent 1 Answer

How to update UI image mid-game (Unity, C#) 2 Answers

c# How Do I disable current script or anyother script in the program within code? 2 Answers

Meaning of .text 1 Answer

Run a function on a Panel from a script on a prefab programically 2 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