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 09:31 PM · c#instantiatearraysnull reference exception

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

I have a null reference exception and this is the exact error : NullReferenceException: Object reference not set to an instance of an object (wrapper stelemref) object:stelemref (object,intptr,object) PlacingParts.Update () (at Assets/Scripts/BuildingRocket/PlacingParts.cs:39). I have a feeling it's the reason my snapping script isn't working ,but I don't know. Here is the script:

 using UnityEngine;
 using System.Collections;
 
 public class PlacingParts : MonoBehaviour 
 {
     public GameObject[] placedParts;
     private int i;
     private Transform currentPart;
     public Transform centerPoint;
 
     private int partLayerMask = 1 << 9;
     private int snapPointLayer = 1 << 8;
 
     private bool hasPlaced;
     public float snapPointDistance = 5f;
 
     void Start () 
     {
         i = 0;
         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))
             {
                     placedParts[i] = currentPart.gameObject;
                     hasPlaced = true;
                     i++;
             }
         
         }
         //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))
         {
             
         } 
         if(placedParts != null && i < placedParts.Length) 
         {
             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" && (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 · Show 8
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 numberkruncher · May 09, 2014 at 09:33 PM 0
Share

I could be wrong, but I think it occurs when attempting to access an element from a null array reference:

 int[] someArray = null;
 someArray[42];
avatar image robertbu · May 09, 2014 at 09:34 PM 0
Share

If you double click on the error, what line does it highlight. There is nothing at line 39 that would cause this error. Is it line 34?

avatar image NutellaDaddy · May 09, 2014 at 09:55 PM 0
Share

Yes, it's line 34, I swear it has something to do with the array!

avatar image NutellaDaddy · May 09, 2014 at 09:58 PM 0
Share

Wait I changed the script a bit here it is again, it is line 39.

 using UnityEngine;
 using System.Collections;
 
 public class PlacingParts : $$anonymous$$onoBehaviour 
 {
     public static GameObject[] placedParts;
     private int i;
     private Transform currentPart;
     public Transform centerPoint;
 
     private int partLayer$$anonymous$$ask = 1 << 9;
     private int snapPointLayer = 1 << 8;
 
     private bool hasPlaced;
     public float snapPointDistance = 5f;
 
     void Start () 
     {
         i = 0;
         hasPlaced = false;
     }
     void Update ()
     {
         //HOW TO PLACE THE ITE$$anonymous$$
         if (currentPart != null && hasPlaced == false)
         {
             //GIVES $$anonymous$$E THE ABILITY TO $$anonymous$$OVE THE PART/OBJECT THAT WAS JUST INSTANTIATED WITH THE PLACEITE$$anonymous$$ FUNCTION
             Vector3 m =  Camera.main.ScreenToWorldPoint (new Vector3(Input.mousePosition.x, Input.mousePosition.y, 12.9f +( $$anonymous$$ouseOrbitImproved.distance - 13.5f)));
             currentPart.position = new Vector3(m.x,m.y,m.z);
 
             //SET THE ITE$$anonymous$$ TO HAS PLACED SO THAT IT STOPS $$anonymous$$OVING
             if(Input.Get$$anonymous$$ouseButtonDown (0))
             {
                     hasPlaced = true;
                     i++;
             }
             if(hasPlaced == true)
             {
                 placedParts[i] = currentPart.gameObject;
             }
         
         }
         //HOW TO PIC$$anonymous$$ THE OBJECT BAC$$anonymous$$ UP AFTER IT HAS BEEN PLACED
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         RaycastHit hit;
         
         if(Physics.Raycast (ray, out hit, $$anonymous$$athf.Infinity, partLayer$$anonymous$$ask))
         {
             
         } 
         if(placedParts != null && i < placedParts.Length) 
         {
             SnapTry(currentPart, placedParts[i].transform);
         }
     }
         void SnapTry(Transform obj1, Transform obj2) {
             //WHEN CALLING THE SCRIPT OBJ1 $$anonymous$$UST BE THE CURRENTPART
             Transform[] transforms1 = obj1.GetComponentsInChildren<Transform>();
             //OBJ2 HAS TO BE AN ARRAY FILLED WITH ALL THE PLACED OBJECTS TRANSFOR$$anonymous$$S
             Transform[] transforms2 = obj2.GetComponentsInChildren<Transform>();
 
             foreach(Transform tr1 in transforms1) {
                 foreach(Transform tr2 in transforms2) {
                     
                 if (tr1.tag == "SnapPoint" && tr2.tag == "SnapPoint" && (tr1.position - tr2.position).sqr$$anonymous$$agnitude <= 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);
     }
 }
 
avatar image tanoshimi · May 09, 2014 at 10:07 PM 0
Share

I'd get rid of the static modifier on the placedParts declaration. Also what are you assigning to that array in the inspector?

Show more comments

1 Reply

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

Answer by robertbu · May 09, 2014 at 10:04 PM

You declare the 'placedParts' variabled, but you never initialize it. That is you have a reference to an array, but you also need to declare a specific amount of space for that array. Something like:

 public static GameObject[] placedParts = new GameObject[100];

That creates 100 slots in your array. If you don't know how big the array will be, you can move to a different collection type. Usually a generic List is a good choice for a variable size array. More info on collection types:

http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use%3F

Comment
Add comment · Show 10 · 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 10:12 PM 0
Share

I am now getting an error saying that my argument is out of range. Here it is : IndexOutOfRangeException: Array index is out of range. (wrapper stelemref) object:stelemref (object,intptr,object) PlacingParts.Update () (at Assets/Scripts/BuildingRocket/PlacingParts.cs:39)

avatar image NutellaDaddy · May 09, 2014 at 10:51 PM 0
Share

Any help for me?

avatar image AlucardJay · May 09, 2014 at 11:13 PM 0
Share

As robertbu said, us List for any array that is changing dynamically in size, not a built-in array.

While you keep incrementing i and never remove elements from the array, you will currently forever get this problem. (I cannot understand how you are trying to use an array here)

avatar image NutellaDaddy · May 09, 2014 at 11:16 PM 0
Share

I am trying to hold all of the items I place into some type of container so I can access them all at once. At some point I will be removing them. I guess I should use a list than.

avatar image NutellaDaddy · May 09, 2014 at 11:24 PM 0
Share

Do you think I should use an arraylist?

Show more comments

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

25 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

Related Questions

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

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

Terrain with lots of objects 1 Answer

How can I make this move in 3d space?[BUILDING SYSTEM] 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