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 /
avatar image
0
Question by BrandonLayton · Aug 14, 2011 at 11:59 PM · javascriptnullreferenceexceptionexception

NullReferenceException help

I am new to Programming in Unity3D (JavaScript) but I have been programming in Java for about a year now and have had experience in other languages, so I consider myself a decent programmer. But recently I have been getting a good amount of errors from Unity that I can not find out. This one has been bothering me the most:

 NullReferenceException: Object reference not set to an instance of an object
 (wrapper stelemref) object:stelemref (object,intptr,object)
 jsStart.BuildPins () (at Assets/jsStart.js:74)
 jsStart.Start () (at Assets/jsStart.js:29)

I understand what it means, but not WHY I am getting it. Here is the code where the error occurs:

 function BuildPins(){
 var currentPin : GameObject;
 for(var i : int = 1; i <= 10;) {
     currentPin = Instantiate(bowlingPin,Vector3(startX,startY,startZ),transform.rotation);
     currentPin.transform.position.y = startY;
     if(i == 1){
         
     } else if (i >= 2 && i <= 3){
         if(i == 2){
             currentPin.transform.position.x = secondX;
             currentPin.transform.position.z = secondZ;
         } else {
             currentPin.transform.position.x = pins[i].transform.position.x;
             currentPin.transform.position.z = -pins[i].transform.position.z;
         }
     } else if (i >= 4 && i <= 6){
         if(i == 4){
             currentPin.transform.position.x = thirdX;
             currentPin.transform.position.z = thirdZ;
         } else {
             currentPin.transform.position.x = pins[i].transform.position.x;
             currentPin.transform.position.z = pins[i].transform.position.z - 0.3368112;
         }
     } else if (i >= 7 && i <= 10){
         if(i == 7){
             currentPin.transform.position.x = fourthX;
             currentPin.transform.position.z = fourthZ;
         } else {
             currentPin.transform.position.x = pins[i].transform.position.x;
             currentPin.transform.position.z = pins[i].transform.position.z - 0.3368112;
         }
     }
     currentPin.transform.rotation.x = 270;
     currentPin.transform.rotation.y = 0;
     currentPin.transform.rotation.z = 0;
     
     pins[i-1] = currentPin;//ERROR IS HERE
     print(i.ToString);
 }
 }

I am trying to make a function that will basically set up a pyramid of Bowling pins in a specific place without having to do too much hardcoding (hence the for statement and subtraction and adding off positions). I don't even get through it once because the debug does not print any numbers (i.ToString).

Any help will be appreciated. :) Thanks, Brandon

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 FTheCloud · Aug 15, 2011 at 12:05 AM

Your problem is with this piece of code:

 var currentPin : GameObject;

You haven't assigned a gameobject to the script in the editor.

Comment
Add comment · Show 4 · 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 BrandonLayton · Aug 15, 2011 at 01:18 AM 0
Share

But I assign it by instantiate dont I? currentPin = Instantiate(bowlingPin,Vector3(startX,startY,startZ),transform.rotation);

avatar image FTheCloud · Aug 15, 2011 at 01:29 AM 0
Share

I see in your instantiate function your spawning "bowlingPin". So that would mean you must have a variable in the beginning of your script presumably named "var bowlingPin : GameObject;"? If so then that means that you must assign an object to that variable in the inspector. It usually doesn't say "NullReferenceException" unless you haven't assigned something to a variable in the inspector.

avatar image BrandonLayton · Aug 15, 2011 at 07:51 PM 0
Share

I definately have a Prefab gameobject assigned to bowlingPin

avatar image FTheCloud · Aug 16, 2011 at 02:18 AM 0
Share

Ohhhhh..... I think its the pins variable, you define it at the end of the script but in unity you have to define it before you use it. Try moving it to the top of your script.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

null reference exception ... help! 2 Answers

NullReferenceException, but the reference is set! 2 Answers

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

A node in a childnode? 1 Answer

NullReferenceException in follow function 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