Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
1
Question by aleichert · Jul 15, 2015 at 03:14 PM · arraynullreferenceexceptionclasssingletonindexoutofrangeexception

Problem with Singleton and NullReferenceException?

EDIT: Problem solved. It actually was super simple. Let this be a lesson to you: When you get confused, get up, take a walk and then come back and re-look at your code. It could save you hours. In this case, it was here:

public Block reinforcedIron = new Block(1, "Reinforced Iron");

This gets executed before Awake(). I had to just put the definition into Start(). So easy!**


Hello there.

I've been working on a game similar to Terraria where you can place "blocks" and break them again. For this I have been trying to make a class for the blocks to easily add new blocks and get information from them later on in the game. This is my "Block" class so far: using UnityEngine; using System.Collections; using System.Collections.Generic;

 public class Block {
 
     public int ID;
     public string Name;
     public Transform Prefab;
 
     public Block(int sentID, string sentName) {
         ID = sentID;
         Name = sentName;
         if(Control.instance.prefabs[ID] != null) {
            Prefab = Control.instance.prefabs[ID];
         } else {
             Debug.LogError ("Warning! There is no prefab for block with ID " + ID);
         }
     }
 
 }
 

I want to later on be able to make a new Block by doing "Block dirt = new Block(1, "Dirt");" for example to make a dirt block. Then the Block class will auto assign all the information for it (e.g. it will search an Array (seen in the next script) for the Transform corresponding to it's ID) and I can get the ID via dirt.ID, the name via dirt.Name and the prefab via dirt.Prefab).

This is my "Control" class: using UnityEngine; using System.Collections; using System.Collections.Generic;

 public class Control : MonoBehaviour {
 
     public static Control instance;
     public static int[,] blocks = new int[999,999];
 
     Vector2 mousePos;
     public Block reinforcedIron = new Block(1, "Reinforced Iron");
     public Transform[] prefabs = new Transform[999];
 
     void Awake() {
         instance = this;
     }
 
     void Start () {
         Debug.Log (prefabs[0].name);
         for(int x=0; x < blocks.GetLength(0); x++) {
             for(int y=0; y < blocks.GetLength(1); y++) {
                 blocks[x,y] = -1;
             }
         }
 
     }
 
     void Update() {
         mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10));
     }
 }
 

It's a Singleton so that I can access the prefab array for prefabs from the Block class and assign the prefabs in the Editor. This way I can search a prefab by its ID in the Block class (see above for more info).

Now I am getting these two Errors when I start the game:

 NullReferenceException: Object reference not set to an instance of an object
 Block..ctor (Int32 sentID, System.String sentName) (at assets/Block.cs:14)
 Control..ctor ()
 
 IndexOutOfRangeException: Array index is out of range.
 Block..ctor (Int32 sentID, System.String sentName) (at assets/Block.cs:14)
 Control..ctor ()
 

Why? What am I doing wrong? For those interested, the "prefab" in my Editor is empty, I however to test added 1 prefab to it (made size to 1 and added a prefab) to see if it can store things. Why is the Block class throwing an IndexOutOfRangeException when I haven't even accessed anything through it yet? And why is there an NullReferenceException when I am using a Singleton?

Thank you, Sincerely, aleichert

Comment
Add comment · Show 1
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 meat5000 ♦ · Jul 15, 2015 at 03:49 PM 0
Share

Please post an answer ins$$anonymous$$d of placing an answer in your question.

0 Replies

· Add your reply
  • Sort: 

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

22 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

Related Questions

Instantiating gameobjects in an array / class problem | NullReferenceException: 0 Answers

Why Again Arrays In ExecuteInEditMode() Gives NullReferenceException Errors?? 1 Answer

"Null Reference Error" when using a custom class as an array 1 Answer

Pushing GameObject into JS Array returns NullReferenceException 1 Answer

C# custom class array error in adding a new entry. 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