• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by ziiion · Jan 18, 2013 at 10:45 PM · nullreferenceexception

need help with null reference

hi first time poster, kinda basic at programming lvl, im getting a:

NullReferenceException: Object reference not set to an instance of an object gridmaker.Start () (at Assets/scripts/gridmaker.js:55)

and i would be tankfull if anybody can help me find the bugger :)

both scripts are in the same game object: pathManager.js

 #pragma strict
 import System.Collections.Generic; //required for list functions
 
 var gridArray = new List.<Transform>(); 
 
 
 
 
 
 public function AdGridBlock(a : Transform){
 
     gridArray.Add(a);
     var _tempLenght = gridArray.Count;
     
     Debug.Log("added: " + a + "array lenght now: " + _tempLenght);
     
 }
 
 
 function Start () {
     
 }
 
 function Update () {
 
 }

and gridMaker.js

 #pragma strict
 // Instantiates a prefab into a grid
 
 // new grid name
 var gridName = "newGrid";
 
 // grid block prefab
 var prefab : GameObject;
 
 // defines _newObj as a GameObject 
 private var _newObj : GameObject;
 
 // create grid at this position
 var initPOSX = 0; //depth
 var initPOSY = 0; //with
 var initPOSZ = 0; //height
 
 var gridX = 5;
 var gridY = 5;
 var spacing = 2.0;
 
 //temp
 private var pathmanager : pathManager;
 private var tempName : Transform;
 
 
 function Start () {
 //
 // create and define our grid holding object
 //
     var _parent = new GameObject (gridName);
     _parent.layer = 9; //select path layer, remove for other purposes
     _parent.transform.position = Vector3(initPOSX, initPOSZ, initPOSY);
 //
 // create the grid and parent the objects to empty _parent object we just created,
 // the grid is created from _parent origin position and naming flows left to right then to the bottom
 //
     for (var x = 0; x < gridX; x++) {
         for (var y = 0; y < gridY; y++) {
             var pos = Vector3 (initPOSX - x * spacing, initPOSZ, initPOSY - y * spacing);
             _newObj = Instantiate(prefab, pos, Quaternion.identity);
             _newObj.name = "x" + x + "y" + y;
             //
             var tempName = _newObj.transform; //
             //Debug.Log(_newObj.name + " created at: Y " + _newObj.transform.position.y + " X " + _newObj.transform.position.x);
             // define _newObj as child of _parent
             _newObj.transform.parent = _parent.transform;
             //
             tempName = _parent.gameObject.transform.FindChild(tempName.name);
             
             // testing comunication with pathManager
             
             //var objName = _newObj.gameObject;
             
             Debug.Log("tempName is: " + tempName);
             pathmanager.AdGridBlock(tempName);
             
         }
     }
     Debug.Log("gridArray has: " + pathmanager.gridArray);
 }
 
 function Update () {
 
 }
Comment
Add comment · Show 4
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 · Jan 18, 2013 at 11:11 PM 0
Share

If you add Debug.Log(pathmanager); above pathmanager.AdGridBlock(blockName); what is echoed to the console?

avatar image ziiion · Jan 19, 2013 at 12:34 AM 0
Share

Debug.Log(pathmanager); returns "null" wich is wierd because the instance it is declared, or should it return null? or is instance of

private var pathmanager : path$$anonymous$$anager;

wrongly declared?

avatar image ziiion · Jan 19, 2013 at 02:02 AM 0
Share

it returns "null" im kinda puzzled about it tho iv been 10h + hitting my head thinking it was tempName causing the problems :/

and this seams, atleast to me that the instance was declared correctly to invoke path$$anonymous$$anager

avatar image numberkruncher · Jan 19, 2013 at 02:20 AM 0
Share

Do you mean that GetComponent(path$$anonymous$$anager) returns null? If so then you will need to add the script to your game object or alternative use AddComponent ins$$anonymous$$d of GetComponent.

1 Reply

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

Answer by numberkruncher · Jan 19, 2013 at 12:39 AM

The variable pathmanager has not been initialized. You should add the following line to your Start() function inside "gridMaker.js":

 pathmanager = GetComponent(pathManager);

Tip: It is generally better to name your classes using the convention PathManager to avoid confusion with variables which are usually named using the convention pathManager. This is of personal taste :-)

Comment
Add comment · Show 2 · 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 ziiion · Jan 19, 2013 at 10:23 AM 0
Share

that did it.

and thanks for the tip, ill take consideration of it in the future :) and you wrote path$$anonymous$$anager in the tip in the same way but i think i get what u mean: class = path$$anonymous$$anager or Path$$anonymous$$anager and variables = _pathmanager or pathmanager

avatar image numberkruncher · Jan 19, 2013 at 02:32 PM 0
Share

class = Path$$anonymous$$anager, variable = path$$anonymous$$anager (public/local) or _path$$anonymous$$anager (private)

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.

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

10 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

Related Questions

Object Reference not set to instance of an object - error in 2d rope code 0 Answers

Getting error "Object reference not set to an instance of an object" 0 Answers

NullReferenceException!! Please Give a Hand 0 Answers

Missing Reference after null Check 4 Answers

Name-fetching script working but with error. 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