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 Nelis · Mar 10, 2013 at 09:58 AM · javascripterrornullreferenceexceptionwrong

What is wrong with my script?

I'm getting a: NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (IConvertible convertible) Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value) Boo.Lang.Runtime.RuntimeServices.UnboxSingle (System.Object value) MapGrid.Drill () (at Assets/Scripts/MapGrid.js:44) MapGrid.Update () (at Assets/Scripts/MapGrid.js:39). And my script is not working. Can someone give me a hint what is wrong?

 var prefab : GameObject;
 var player : GameObject;
 var gridX = 5;
 var gridY = 5;
 private var spacing = 1;
 var UseSpawner : boolean = true;
 var Spawn : GameObject;
 
 var gridXRounded;
 var SpawnX;
 var SpawnY;
 var block : GameObject;
 
 function Start () 
 {
     grid2D = new GameObject[gridX,gridY];
     for (var y = 0; y < gridY; y++){
         for (var x = 0; x < gridX; x++){
             var pos : Vector3 = Vector3(x,y,0) * spacing;
             var newBlock : GameObject = Instantiate(prefab, pos, Quaternion.identity);
             newBlock.name = "GridBlock" + x + "-" + y;
 
         }
     }
             
     if(UseSpawner){
         var SpawnX : int = Mathf.Round(gridX / 2 - 1);
         var SpawnY : float = gridY + 0.5;
 
         player.transform.localScale = Vector3(0.75, 0.75, 0.75);
         Spawn.transform.position = Vector3(SpawnX, SpawnY, 0 );
         player.transform.position = Vector3(SpawnX, SpawnY, 0);
     }
 }
 
 function Update() {
     if(Input.GetButton("Down")){
         Drill();
     }
 }
     
 function Drill(){
     block = GameObject.Find("GridBlock" + Mathf.Round(player.transform.x) + "-" + Mathf.Round(player.transform.y - 1));
     Destroy(block);
 }
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
3
Best Answer

Answer by Khada · Mar 10, 2013 at 10:19 AM

It would seem that 'block' is null inside your 'Drill' method. Put a break point on line line 44 and check it's value. It may be that your character is attempting to drill at a location where there is no block to destroy, thus you should add a sanity check inside your 'Drill' method:

 function Drill()
 {
     block = GameObject.Find("GridBlock" + Mathf.Round(player.transform.x) + "-" + Mathf.Round(player.transform.y - 1));
 
     if(block != null)
         Destroy(block);
 }

EDIT 1:

It also looks like your 'player' object may be null as your script doesn't assign it anywhere in the code you provided. You should assign it in the 'Start' method.

EDIT 2:

Further inspection makes me think that you need to cast your float values to ints as you seem to have used ints when naming the blocks. Line 44 should look like:

 block = GameObject.Find("GridBlock" + parseInt(Mathf.Round(player.transform.x)) + "-" + parseInt(Mathf.Round(player.transform.y - 1)));
Comment
Add comment · Show 16 · 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 Nelis · Mar 10, 2013 at 10:28 AM 0
Share

I have added it to my script but it's still not working. It is also giving me this error again: NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (IConvertible convertible) Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value) Boo.Lang.Runtime.RuntimeServices.UnboxSingle (System.Object value) $$anonymous$$apGrid.Drill () (at Assets/Scripts/$$anonymous$$apGrid.js:44) $$anonymous$$apGrid.Update () (at Assets/Scripts/$$anonymous$$apGrid.js:38)

avatar image Khada · Mar 10, 2013 at 10:39 AM 0
Share

Actually, it looks like it may be the player object that is null as I don't see you assigning it anywhere (or it could be both). Do you set player somewhere else? If not, you need to assign it before you use it.

avatar image Nelis · Mar 10, 2013 at 10:42 AM 0
Share

I move my player around, so i guess it could be. But how do you assign it?

avatar image Khada · Mar 10, 2013 at 10:48 AM 0
Share

Well you can either assign it in the inspector (which you may already be doing) or you can use GameObject.Find to get it by name/tag etc.

Seriously though, use a breakpoint to check all the variables on line 44 and see which one is null.

Here is info on debugging if you need. Basically just attack the unity process to your mono program, press f9 with the cursor on line 44 of that script and run the game. Hover the mouse cursor in mono to see the value of an object.

avatar image Nelis · Mar 10, 2013 at 12:39 PM 0
Share

I did that and it says the the cube = {null}, but I have no idea how to fix that...

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

11 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

Related Questions

enemy mechanics script and declining health 1 Answer

Windows Script error upon opening a new javascript behavior script. 4 Answers

Problems with java script. 1 Answer

Error in script 1 Answer

syntax errors 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