• 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 /
This question was closed Feb 14, 2016 at 10:41 PM by hexagonius for the following reason:

http://docs.unity3d.com/Manual/NullReferenceException.html

avatar image
0
Question by nathanael_cassagnol · Feb 14, 2016 at 09:21 PM · nullreferenceexception

Null Reference Exception on C# script

Im trying to get this pickup script working and I keep getting the Null Reference Exception Error for line 28

"NewWeapon.transform.SetParent(player.transform);"

As you can see New Weapon and player are both found in the script. New weapon is created and player is the object the script is attached to. So I cannot for the life of me figure whats coming out null.

If somebody could help me solve this it would be greatly appreciated.

using UnityEngine; using System.Collections; public class SwitchWithPickup : MonoBehaviour { %|-762413517_1|% %|-1848002003_2|% %|1814450058_2|% %|-905520308_3|% %|-805487823_5|% %|1668055449_6|% %|-2081906248_4|% %|-912623362_5|% %|-1988582474_6|% %|-523839565_7|% %|-1510337148_8|% %|-1690468637_9|% %|-1926582400_13|% %|882512063_1|% %|-1473455472_11|% %|-1003367730_16|% %|-1860349403_17|% %|-237406474_18|% %|308905561_19|% }

Comment
Add comment · Show 2
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 gjf · Feb 14, 2016 at 01:09 AM 0
Share

please repost your code and format it properly.

avatar image nathanael_cassagnol · Feb 14, 2016 at 03:24 AM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class SwitchWithPickup : $$anonymous$$onoBehaviour {
 
     public string childThatWillBeReplaced;
     GameObject weapon;
     GameObject pickupTrigger;
     GameObject player;
 
 
     void Start () {
         weapon = GameObject.Find (childThatWillBeReplaced);
         player = this.gameObject;
     }
 
     void OnTriggerEnter(Collider other) {
         pickupTrigger = other.gameObject;
 
         if (other.CompareTag("pickup"))
         {
             other.tag="Weapon";
 
             Destroy(weapon);
 
             GameObject NewWeapon = Instantiate(other, transform.position, Quaternion.identity) as GameObject;
 
             NewWeapon.transform.SetParent(player.transform);
 
             Destroy(other.transform.gameObject);
 
         }
     }
 }

0 Replies

  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

[Solved]unexplanable null refrence exception 4 Answers

Inventory problem 0 Answers

Faulty NullReferenceException? 2 Answers

Why cant I instantiate???? 2 Answers

[solved] This error "NullReferenceException: Object reference not set to an instance of an object" occurs only the first time? 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