Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by ltrout1999 · Oct 12, 2016 at 01:27 AM · nullreferenceexceptionnull reference exceptionobject-reference-error

Unknown Cause of NullReferenceException?

So I was going through a question I asked awhile ago because I needed a script someone helped me make off of it and found it and was going to use it in my game, but I'm getting a NullReferenceException error.

Here is the error:

 NullReferenceException: Object reference not set to an instance of an object
 GridCreator.Place () (at Assets/Scripts/GridCreator.cs:27)

My code for the GridCreator.cs is this: using UnityEngine; using System.Collections; using UnityEditor;

 public class GridCreator : MonoBehaviour {
 
     [MenuItem("Grid Creator/Create the Grid (9x13)")]
     static void Place()
     {
         // change these to work for you:
         string prefabName = "AnchorPoint"; // case sensitive
         Vector3 gridSize = new Vector3(9, 1, 13); // ie. (10, 1, 5) will generate 10 cubes in the x direction, 1 in the y, and 5 in the z
         Vector3 offset = new Vector3(1, 1, 1); // size of the cubes - default is (1, 1, 1)
         string newCubeName = "AnchorPoint"; // leave blank for default name eg. "Cube(Clone)"
 
 
         GameObject cube = Resources.Load(prefabName, typeof(GameObject)) as GameObject;
 
 
         for (int x = 0; x < gridSize.x; ++x)
         {
             for (int y = 0; y < gridSize.y; ++y)
             {
                 for (int z = 0; z < gridSize.z; ++z)
                 {
                     Vector3 pos = new Vector3(x * offset.x, y * offset.y, z * offset.z);
                     GameObject go = Instantiate(cube, pos, cube.transform.rotation) as GameObject;
 
                     if (newCubeName != "")
                         go.name = newCubeName;
                 }
             }
         }
     }
 }

I am confused as to why it is suddenly not working when it did last time I used it. Only difference is the prefab I'm using. I was using a cube, now I'm using a sphere, but that shouldn't matter.

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 ltrout1999 · Oct 12, 2016 at 01:41 AM

Nevermind, I fixed my issue! Forgot I had to put "/Prefabs/(insert prefab name here)" as the string prefabName.

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.

Follow this Question

Answers Answers and Comments

75 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

Related Questions

NullReferenceException: Object reference not set to an instance of an object 1 Answer

Why cant i instantiate my ship? Error NullReferenceException: Object reference not set to an instance of an object. 1 Answer

Input Working in Editor but not in Build 0 Answers

NullReferenceException after build 1 Answer

Need some help with networking, Object reference not set to an instance of an object. 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