• 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 /
This question was closed Aug 20, 2017 at 06:10 PM by hexagonius for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by TheLeadHound · Aug 20, 2017 at 04:03 PM · spritenullreferenceexception

Why am I getting a NullReferenceException while trying to set a Sprite? (C#)

I created a tool for automatically generating a group of hexes. It can successfully create, name, and place the GameObjects in the right place. However, when I tried to make it give the objects a default sprite, I kept getting a null reference error on line 17 which just says HexImage =. If I get rid of this part of the code, the hex objects each get an empty Sprite Renderer. Is there a way to assign he default sprite other than this, or is there some way I can resolve this issue?

Here is the error I am getting:

 NullReferenceException: Object reference not set to an instance of an object
 HexGenerator.Create () (at Assets/Editor/HexGenerator.cs:17)

This is the code I am using:

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 
 public class HexGenerator 
 {
     [MenuItem("Hex Board/Generate Tiles")]
     static void Create()
     {
         int y = 6;
         int x = 6;
         float Xdist = 1.328f; //The x distance between adjacent tiles
         float Ydist = 1.016f; //The y distance between adjecent tiles
         //GameObject Container = new GameObject("Hexes");
         GameObject HexBlueprint;
         Sprite HexImage = new Sprite();
         HexImage = 
             Sprite.Create(Resources.Load<Texture2D>("Assets/Base Hex.png"),
             new Rect(0, 0, Resources.Load<Texture2D>("Assets/Base Hex.png").width, Resources.Load<Texture2D>("Assets/Base Hex.png").height),
             new Vector2(0.5f, 0.5f));
 
         for (int i = 0; i < y; i++)
         {
             for (int j = 0; j < x; j++)
             {
                 if (i % 2 == 0)
                 {
                     HexBlueprint = new GameObject("Hex " + j + "," + i);
                     HexBlueprint.transform.position = new Vector3(j * Xdist, i * Ydist);
 
                     SpriteRenderer renderer = HexBlueprint.AddComponent<SpriteRenderer>();
                     renderer.sprite = HexImage;
                 }
                 else
                 {
                     HexBlueprint = new GameObject("Hex " + j + "," + i);
                     HexBlueprint.transform.position = new Vector3(j * Xdist + Xdist / 2, i * Ydist);
 
                     SpriteRenderer renderer = HexBlueprint.AddComponent<SpriteRenderer>();
                     renderer.sprite = HexImage;
                 }
             }
         }
     }
 }


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

  • Sort: 
avatar image
0
Best Answer

Answer by hfiani · Aug 20, 2017 at 04:06 PM

Resources.Load works starting from a folder Called: Resources. so create a folder called : Resources in Assets and put the files in it and then use this instead:

 Resources.Load<Texture2D>("Base Hex")

Note that the .png is removed, so NEVER put two files with same name but different extension in the same folder.

personally i create inside Resources folders like: Textures, Materials, Scripts, Scenes etc... if so then put your pong files inside textures inside Resources inside Assets and use:

 Resources.Load<Texture2D>("Textures/Base Hex")

hope this helps

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
avatar image
0

Answer by TheLeadHound · Aug 20, 2017 at 04:31 PM

God bless you. All the places I read from had not mentioned you needed to have a folder called Resources to use Resources.Load. I really appreciate this!

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 hfiani · Aug 20, 2017 at 04:34 PM 0
Share

no worries. glad it helped. i $$anonymous$$ch basic unity in the university, so if you need any other help, let me know, then maybe i can help.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Cannot access sprite of a gameobject 1 Answer

Problem with Image and Sprite 1 Answer

NullReferanceException layerMask script c# to unityscript. 0 Answers

I have issue with unitys serialization 1 Answer

NullReferenceException: Object reference not set to n instance of an object script. 2 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