Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by RoleyBaxter · May 12, 2014 at 01:08 AM · instantiatenullreferenceexception

NullReferenceException

Hi I'm making a 2D platformer with guns.

I'm having trouble with my weapon pickup scripts. I want to 1. Give player a new weapon (gameObject). 2. Destroy the old weapon. 3. make references for the old weapon point to the instance of the new weapon.

I have a "WeaponPickup" class that hold a reference to the prefab. I want to pass this to a function on my "PlayerActions" -script.

It looks like this:

 public void GiveWeapon(GameObject newWeapon)
     {
         
         Debug.Log(newWeapon);
         GameObject gunClone;
         gunClone = Instantiate(newWeapon, gunAP.position, Quaternion.identity) as GameObject;
         if (!facingRight)
             newWeapon.transform.localScale = new Vector3(-newWeapon.transform.localScale.x, newWeapon.transform.localScale.y, newWeapon.transform.localScale.z);
         gun = newWeapon;
         gun.transform.parent = gunAP;
         gunProperties = gun.GetComponent<GunProperties>();
         gP1 = gun.transform.Find("GP1");
         
     }         

I get this error: NullReferenceException: Object reference not set to an instance of an object NewPlayerController.GiveWeapon (UnityEngine.GameObject newWeapon) (at Assets/Scripts/Player/NewPlayerController.cs:389) WeaponPickup.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/WeaponPickup.cs:35)

Essentially I want to replace a child of my PlayerCharacter with a Prefab and have the new prefab "stick to" (become child of) my Player's Arm-GameObject.

PS the Debug.Log above correctly posts the name of the new Weapon GameObject passed from my "WeaponPickup" class.

Comment
Add comment · Show 3
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 jacobschellenberg · May 12, 2014 at 01:29 AM 0
Share

Is gunAP assigned? Oh actually... I just saw, you instantiate GameObject gunClone as a GameObject, and a few lines down you're doing a GetComponent, it would seem to me, that at this point the gun doesn't actually have a GunProperties script on it, which is a probable cause for the null reference.

avatar image RoleyBaxter · May 12, 2014 at 06:33 PM 0
Share

The prefab has the GunProperties script attached to it. The instanced clone should also have it no?

avatar image RoleyBaxter · May 12, 2014 at 08:08 PM 0
Share

gunAP was not assigned properly, this caused the NullReference. Thanks for helping!

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer Wiki

Answer by Kiwasi · May 12, 2014 at 07:25 PM

If line 4 returns a gun then your problem is not newWeapon.

Your problem is probably gunAP.position. Throw that into a debug statement and see what happens.

Things to check

  • Does gunAP exist?

  • Is gunAP a Transform?

  • Has gunAP been assigned a value?

  • Is gunAP declared in this script?

  • Is gunAP out of scope? Out of scope typically happens when it is declared inside another method. In general variables only exist until the close of the curly brackets they were declared in.

  • Has gunAP been destroyed by some other script?

Comment
Add comment · Show 1 · 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 RoleyBaxter · May 12, 2014 at 07:55 PM 1
Share

Thanks, this was indeed the problem. gunAP was null, I had set it earlier using transform.find which returned null, since it was a child of a child of a child. I've set it ins$$anonymous$$d using a tag and that solved everything. Should've Debug.log'ed it but I was staring myself blind at the 'newWeapon' to be the culprit. $$anonymous$$uch appreciated!

avatar image
0

Answer by Warenth · May 12, 2014 at 02:45 AM

From what I can see, it appears you are referencing newWeapon when you should be referencing gunClone. On this line:

gun.transform.parent = gunAP;

If gun is newWeapon, since you set it as so:

gun = newWeapon;

Then gun.transform might be null if newWeapon is a prefab that is not actually in your scene.

My guess is this script should be referencing gunClone instead of newWeapon after new weapon has been cloned.

Comment
Add comment · Show 1 · 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 RoleyBaxter · May 12, 2014 at 07:08 AM 0
Share

Thanks for your answer, this is true, but still does not help. The error occurs before at:

gunClone = Instantiate(newWeapon, gunAP.position, Quaternion.identity) as GameObject;

my code is now:

 public void GiveWeapon(GameObject newWeapon)
     {
  
         Debug.Log(newWeapon);
         GameObject gunClone;
         gunClone = Instantiate(newWeapon, gunAP.position, Quaternion.identity) as GameObject;
         if (!facingRight)
             gunClone.transform.localScale = new Vector3(-gunClone.transform.localScale.x, gunClone.transform.localScale.y, gunClone.transform.localScale.z);
         gun = gunClone;
         gun.transform.parent = gunAP;
         gunProperties = gun.GetComponent<GunProperties>();
         gP1 = gun.transform.Find("GP1");
  
     }         


Still same NullReferenceException. Is it somehow impossible to Instatiate a prefab that is passed from another class through a public function?

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

22 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

Related Questions

Material[] Object reference not set when instantiating 2 Answers

Variable Assigning with GameObject.Find("") causing NullReferenceException? 0 Answers

Simultaneous Null Reference Exception and expected value 1 Answer

Accessing a prefab after instantiating results as null 1 Answer

NullReferenceException after instantiating. 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