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 /
  • Help Room /
avatar image
0
Question by Granite Bear · Nov 01, 2015 at 04:13 PM · c#unity 52d gamenullreferenceexceptiontutorial

NullReferenceException: Object reference not set to an instance of an object, again

I am a beginner to Unity C#, but not C# itself. I have seen this topic several times here but none seem to answer my questions, PLEASE help! This class is the location of the error.

 using UnityEngine;
 using System.Collections;
 public class WorldGen : MonoBehaviour {

 private BlockManager blockManager;

 private int width = 64;
 private int height = 128;

 private Block[,] blocks;

 public GameObject GameManager;

 private void Start()
 {
     blockManager = GameObject.Find("GameManager").GetComponent<BlockManager>();
     blocks = new Block[width, height];

     GenerateBlocks();
     SpawnBlocks();
 }

 private void GenerateBlocks()
 {
     for (int x = 0; x < width; x++)
     {
         for(int y = 0; y < height; y++)
         {
             blocks[x, y] = blockManager.FindBlock(2);
         }
     }
 }

 private void SpawnBlocks()
 {
     for (int x = 0; x < width; x++)
     {
         for (int y = 0; y < height; y++)
         {
             GameObject blockGO = new GameObject();
             SpriteRenderer sr = blockGO.AddComponent<SpriteRenderer>();
             sr.sprite = blocks[x, y].sprite;
             blockGO.name = blocks[x, y].display_name;
             blockGO.transform.position = new Vector3(x, y);

         }
     }
 }
 }

Originally I had followed a tutorial exactly as far as I could tell, but they did not get this error, and I did. Tutorial: https://www.youtube.com/watch?v=wqivozn6Sgc&list=PLyJRgNGLgSoW7iGxKI5ZAL0IfGrlqMeG9∈dex=3

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Glurth · Nov 01, 2015 at 04:35 PM

We will need to know which line generates the error, to help you find it, but here is some info that will help you find this one yourself, and others in the future.

This error means that your code is attempting to access a member variable, or function, for an object that has not been created yet (is still null).

So, for example: Lets say this function calls FAILS to find the object you are looking for:

 GameObject.Find("GameManager")

If it fails to find the object, it will return a NULL value. So, when you try to access the GetComponent function like so:

 GameObject.Find("GameManager").GetComponent<BlockManager>();

it would generate a null reference exception, because we are in effect saying:

 null.GetComponent<BlockManager>();

which doesn't make much sense.

The safer way to do this would be like so:

     GameObject foundObject = GameObject.Find("GameManager");
     if(foundObject!=null)
        foundObject.GetComponent<BlockManager>();
     else 
        Debug.LogError("No block manager found in scene");


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 Granite Bear · Nov 01, 2015 at 06:23 PM 0
Share

Thanks! So I added what you suggested, and it is not the source of the error, this is:

 private void GenerateBlocks()
 {
     for (int x = 0; x < width; x++)
     {
         for(int y = 0; y < height; y++)
         {
             blocks[x, y] = block$$anonymous$$anager.FindBlock(2);
         }
     }
 }


Or more specifically, blocks[x, y] = block$$anonymous$$anager.FindBlock(2);

The full error message, in case you want it, is:

 NullReferenceException: Object reference not set to an instance of an object
 WorldGen.GenerateBlocks () (at Assets/BurnseyEntertainment/Scripts/WorldGen.cs:39)
 WorldGen.Start () (at Assets/BurnseyEntertainment/Scripts/WorldGen.cs:29)
avatar image Glurth Granite Bear · Nov 01, 2015 at 06:52 PM 0
Share

ok, so the error is saying that this line:

 blocks[x, y] = block$$anonymous$$anager.FindBlock(2);

when running, is co$$anonymous$$g up as, either:

 blocks[x, y] = null.FindBlock(2);

or

 null[x, y] = block$$anonymous$$anager.FindBlock(2);

or BOTH.

 null[x, y] = null.FindBlock(2);
avatar image
0

Answer by Granite Bear · Nov 01, 2015 at 07:02 PM

Answer Found! The GameObject "Manager" was missing the script required for the code to run. Once it was replaced... It worked! Wonderfully! Check your scripts everyone!

Comment
Add comment · 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

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

40 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 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

Level Selector - Level Images for Brick Breaker 2 Answers

Im using the tutorial on ''Moving the player'' and many of the codes he use in the tutorial don't work at all when i try. 3 Answers

UFO Tutorial, after pickup vid, my player ignores the wall collisions 0 Answers

Why is my component coming back as null? 1 Answer

New to Unity and Design, need some help :( 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