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 dillon_kneeland · Jul 09, 2014 at 04:34 PM · objectreferencestaticexceptionblock

Object Reference Exception (I'm getting sick of these) :P

For the life of me I can't find the error in this code. Apparently heldBlock is not being set but I can't find the error.

 using UnityEngine;
 using System.Collections;
 
 public class BlockPlacer : MonoBehaviour
 {
 
     private GameManager.Block[] blocks = new GameManager.Block[GameManager.blockSet.Length];
 
     public float reach;
 
     private int i = 0;
 
     private GameManager.Block heldBlock;
 
     private AudioSource[] sounds = new AudioSource[2];
     private AudioSource placeBlock;
     private AudioSource breakBlock;
 
     void Start()
     {
 
         int i = 0;
         foreach (GameManager.Block blck in GameManager.blockSet)
         {
             blocks[i] = blck;
             i++;
         }
 
         sounds = GetComponents<AudioSource>();
         placeBlock = sounds[0];
         breakBlock = sounds[1];
 
 
         heldBlock = blocks[0];
     }
 
     // Update is called once per frame
     void Update()
     {
 
         RaycastHit hitInfo;
 
         //Creating a block
         if (Input.GetButtonDown("Fire1"))
         {
             Ray direction = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
 
             if (Physics.Raycast(direction, out hitInfo, reach))
             {
                 Transform oHit = hitInfo.collider.gameObject.transform;
                 Instantiate(heldBlock.block, new Vector3(oHit.position.x + hitInfo.normal.x, oHit.position.y + hitInfo.normal.y, oHit.position.z + hitInfo.normal.z), Quaternion.identity);
                 placeBlock.Play();
             }
         }
 
         //Deleting a block
         else if (Input.GetButtonDown("Fire2"))
         {
             //Get the block to be deleted
             Ray direction = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
             if (Physics.Raycast(direction, out hitInfo))
             {
                 //Make sure it can be broken
                 if (hitInfo.collider.gameObject.tag == "breakable")
                 {
                     //Break the block
                     Destroy(hitInfo.collider.gameObject);
                     breakBlock.Play();
                 }
             }
         }
 
         //Changing held block
         if (Input.GetAxis("Mouse ScrollWheel") > 0)
         {
             i++;
             if (i <= (blocks.Length - 1))
             {
                 heldBlock = blocks[i];
             }
             else if (i > blocks.Length)
             {
                 i = 0;
                 heldBlock = blocks[i];
             }
         }
         else if (Input.GetAxis("Mouse ScrollWheel") < 0)
         {
             i--;
             if (i >= 0)
             {
                 heldBlock = blocks[i];
             }
             else if (i < 0)
             {
                 i = (blocks.Length - 1);
                 heldBlock = blocks[i];
             }
         }
 
     }
 
     void OnGUI()
     {
         GUI.Box(new Rect(10, 10, heldBlock.blockTex.width, heldBlock.blockTex.height), heldBlock.blockTex, GUIStyle.none);
     }
 
 }

the error is being thrown in OnGUI()

It's a lot of code but you need it all if you're gonna find it.

I'm getting three separate errors. The first one happens about 5 five times before I stop the game.

NullReferenceException: Object reference not set to an instance of an object BlockPlacer..ctor ()

the second and third are both logged only once

NullReferenceException: Object reference not set to an instance of an object WorldGeneration.Start () (at Assets/Scripts/WorldGeneration.cs:15)

NullReferenceException: Object reference not set to an instance of an object (wrapper stelemref) object:stelemref (object,intptr,object) BlockPlacer.Start () (at Assets/Scripts/BlockPlacer.cs:25)

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 Landern · Jul 09, 2014 at 04:37 PM

From you code it can only be properties in your Block type.

Since OnGUI only has one line, here are your options: heldBlock is null leaving blockText inaccessible heldBlock.blockTex is null leaving blockText.blockTex inaccessible heldBlock.blockTex.width shouldn't be nullable and default to 0, so that shouldn't be the issue.

The same is true for the fourth(height) parameter of Rect's constructor.

My guess is that heldBlock.blockTex is null and not set/instantiated.

Also, actually post a copy of the exception that is thrown, it's a lot more helpful then your title.

Comment
Add comment · Show 1 · 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 dillon_kneeland · Jul 09, 2014 at 05:14 PM 0
Share

Thanks It was actually a problem with load order and is too complicated for me to explain lol thanks anyway.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

C# Null Object Reference - Driving me mad! 1 Answer

Object Pool object reference is null? 1 Answer

Do you have to set a object reference for every script? 0 Answers

Understanding static methods and object referencing. 3 Answers

object not set to instance? 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