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 NutellaDaddy · May 09, 2014 at 03:53 AM · functionarraysnull reference exception

NullReferenceException: Object reference not set to an instance of an object PlacingParts.Update () (at Assets/Scripts/BuildingRocket/PlacingParts.cs:46)

I am getting an error where I call the function SnapTry. I think it has something to do with my array that I have. Can anyone help me find out the problem? If you need me to tell you about what the script does than I can and maybe I can find a better way to achieve what I'm trying to if this won't work. Here's the script:

 using UnityEngine;
 using System.Collections;
 
 public class PlacingParts : MonoBehaviour 
 {
     public static GameObject[] placedParts;
     private int i = 0;
     private Transform currentPart;
     public Transform centerPoint;
 
     private int partLayerMask = 1 << 9;
     private int snapPointLayer = 1 << 8;
 
     private bool hasPlaced;
     public float snapPointDistance = 0.75f;
 
     void Start () 
     {
         hasPlaced = false;
     }
     void Update ()
     {
         //HOW TO PLACE THE ITEM
         if (currentPart != null && hasPlaced == false)
         {
             //GIVES ME THE ABILITY TO MOVE THE PART/OBJECT THAT WAS JUST INSTANTIATED WITH THE PLACEITEM FUNCTION
             Vector3 m =  Camera.main.ScreenToWorldPoint (new Vector3(Input.mousePosition.x, Input.mousePosition.y, 12.9f +( MouseOrbitImproved.distance - 13.5f)));
             currentPart.position = new Vector3(m.x,m.y,m.z);
 
             //SET THE ITEM TO HAS PLACED SO THAT IT STOPS MOVING
             if(Input.GetMouseButtonDown (0))
             {
                     hasPlaced = true;
                     placedParts[i + 1] = currentPart.gameObject;
             }
         
         }
         //HOW TO PICK THE OBJECT BACK UP AFTER IT HAS BEEN PLACED
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         RaycastHit hit;
         
         if(Physics.Raycast (ray, out hit, Mathf.Infinity, partLayerMask))
         {
         
         } 
         SnapTry (currentPart, placedParts [i].transform);
     }
         void SnapTry(Transform obj1, Transform obj2) {
             //WHEN CALLING THE SCRIPT OBJ1 MUST BE THE CURRENTPART
             Transform[] transforms1 = obj1.GetComponentsInChildren<Transform>();
             //OBJ2 HAS TO BE AN ARRAY FILLED WITH ALL THE PLACED OBJECTS TRANSFORMS
             Transform[] transforms2 = obj2.GetComponentsInChildren<Transform>();
 
             foreach(Transform tr1 in transforms1) {
                 foreach(Transform tr2 in transforms2) {
                     
                     if (tr1.tag == "SnapPoint" && tr2.tag == "SnapPoint") {
                         if ((tr1.position - tr2.position).sqrMagnitude <= snapPointDistance) {
                             Debug.Log ("You could snap an object here if the script was finsihed");
                         }
                     }
                 }
             }
         }
 
     public void PlaceItem(GameObject b)
     {
         hasPlaced = false;
         currentPart = (((GameObject)Instantiate(b)).transform);
     }
 }
 
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
Best Answer

Answer by KellyThomas · May 09, 2014 at 04:01 AM

placedParts is null when executing line 46:

 SnapTry (currentPart, placedParts [i].transform);

You will need to initialize placedParts or implement manual null and bounds checking.

Comment
Add comment · 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 NutellaDaddy · May 09, 2014 at 04:09 AM 0
Share

How could I do this when placedParts starts out with 0 objects in it and none get put into it until you place an object?

avatar image KellyThomas · May 09, 2014 at 04:18 AM 0
Share

That problem is the cause of the error! If it has no items then how can you access the first?

If you are happy with the logic/workflow of the rest then you can just add some sanity checks:

 if(placedParts != null && i < placeParts.Length) {
     SnapTry(currentPart, placedParts[i].transform);
 }
avatar image NutellaDaddy · May 09, 2014 at 04:28 AM 0
Share

Thank you good sir!

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

21 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

Related Questions

I have never seen a null reference exception like this one! 1 Answer

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

NullReferenceException: Object reference not set to an instance of an object 2 Answers

Problem with array on gameobjects 2 Answers

Possible to play single clips using Mecanim? 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