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
1
Question by Magnomous · Dec 27, 2013 at 11:27 AM · c#instantiateresource.load

Instantiate prefab using Resources.Load()

Hello, I am trying to instantiate cube(block) depending on which of them is selected in List, but unity writes

"NullReferenceException: Object reference not set to an instance of an object Building.BlockName () (at Assets/Scripts/Building.cs:33) Building.Update () (at Assets/Scripts/Building.cs:21) "

Building script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Building : MonoBehaviour {
 
     
     private Transform _spawnPosition;
     private string _blockName;
     
     void Update() {
 
         _blockName = BlockName();
 
         if(Input.GetKeyUp(KeyCode.Mouse0)) {
             
             Instantiate(Resources.Load(_blockName), _spawnPosition.position, Quaternion.identity);
         }
     }
     
     string BlockName() {
 
         MainBar mb = GetComponent<MainBar>();
         
         return mb.blocks[mb.selectedItem].Name;
     }
 }
 



And here is MainBar script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class MainBar : MonoBehaviour {
     
     private float _barWidth;
     private float _barHeight;
     private int _offset;
     private int _barSlots;
     
     private float _iconSize;
     private int _iconOffset;
         
     public List<Block> blocks;
     public int selectedItem;    
 
     // Use this for initialization
     void Start () {
         
         _barWidth = Screen.width;
         _barHeight = Screen.height/10;
         _offset = 100;
 
         _barSlots = 3;
         _iconOffset = 10;
         _iconSize = _barHeight - _iconOffset; 
 
         blocks = new List<Block>();
         selectedItem = 0;
 
         //for now we want to add wooden, stone, bricks block to main bar
         AddBlock("WoodenBlock", Icons.woodBlockIcon);
         AddBlock("StoneBlock", Icons.stoneBlockIcon);
         AddBlock("BricksBlock", Icons.bricksBlockIcon);
     }
     
     void AddBlock(string name, Texture2D icon) {
         
         Block block = new Block(name, icon);
         blocks.Add(block);    
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void OnGUI() {
         
         GUI.Box(new Rect(_offset, Screen.height - _barHeight, _barWidth - 2*(_offset), _barHeight), string.Empty);
 
         for(int i = 0; i < _barSlots; i++) {
             
                 if(GUI.Button(new Rect(_offset + i *(_iconOffset + _iconSize), Screen.height - _iconSize - _iconOffset/2, _iconSize, _iconSize), blocks[i].Icon)) {
                     selectedItem = i;
                 }
         }
     }
 }

And very simple Block class:

 using UnityEngine;
 
 public class Block : MonoBehaviour {
 
     private string _name;
     private Texture2D _icon;
 
     public Block(string name, Texture2D icon) {
         
         _name = name;
         _icon = icon;
     }
 
 #region Getters and Setters
     public string Name {
         get { return _name; }
         set { _name = value; }
     }
 
     public Texture2D Icon {
         get { return _icon; }
         set { _icon = value; } 
     }
 #endregion
 
 }


I will be grateful for any help.

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
1
Best Answer

Answer by KellyThomas · Dec 27, 2013 at 01:23 PM

I you have unexpected null references and can't see where they are coming from attaching a debugger and stepping though the code is often the best strategy.

Another strategy is to test any assumptions and intermediate results and print step by step updates to the console or log.

Please try replacing the BlockName method with this code and check the console for messages.

 string BlockName() {
     MainBar mb = GetComponent<MainBar>();

     if(mb == null) {
         Debug.Log("MainBar not found");
         return "Error";
     }

     if(mb.blocks == null) {
         Debug.Log("mb.blocks is null");
         return "Error";
     }

     Debug.Log("mb.selectedItem: " + mb.selectedItem);
     Debug.Log("mb.blocks.Count: " + mb.blocks.Count);

     Block block = mb.blocks[mb.selectedItem];
     if (block == null) {
          Debug.Log("block is null");
          return "Error";
     }
     Debug.Log("block.Name: " + block.Name);

     return block.Name;
 }
Comment
Add comment · Show 3 · 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 Magnomous · Dec 27, 2013 at 01:52 PM 0
Share

$$anonymous$$ainBar not found.

avatar image KellyThomas · Dec 27, 2013 at 02:00 PM 1
Share

Bingo! Is $$anonymous$$ainBar attached to the same GameObject or another?

avatar image Magnomous · Dec 27, 2013 at 02:07 PM 0
Share

How could I made such a mistake :D. Thanks a lot both of you!

avatar image
1

Answer by Tomer-Barkan · Dec 27, 2013 at 12:31 PM

  1. Make sure that the string passed to resources.load is indeed an actual prefab name. You can add a debug log just before and print _blockName.

  2. Use the generic version of Resources.Load: Resources.Load()

  3. Make sure that your prefabs are under a directory named "Resources". They must be under a Resources directory to use Resources.Load.

  4. Make sure that spawnPosition is correctly defined and not null.

      void Update() {
     
             _blockName = BlockName();
     
             if(Input.GetKeyUp(KeyCode.Mouse0)) {
                 Debug.Log("Instantiating prefab: " + _blockName + " at position " + _spawnPosition.position);
                 Instantiate(Resources.Load<GameObject>(_blockName), _spawnPosition.position, Quaternion.identity);
             }
         }
    
Comment
Add comment · Show 3 · 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 Magnomous · Dec 27, 2013 at 12:50 PM 0
Share

Point 4. and 3. are correctly as long as it works for one, specific object. But strange is, that when I added Debug.Log into it as you said, it doesn't even appear. Now I really don't know what is wrong.

avatar image Magnomous Magnomous · Dec 27, 2013 at 01:00 PM 0
Share

Now I added

         $$anonymous$$ainBar mb = GetComponent<$$anonymous$$ainBar>();
         Debug.Log(mb.selectedItem);

to very start of Update function and it didn't appear too. There must be problem in instantiating class...

avatar image Tomer-Barkan · Dec 27, 2013 at 02:00 PM 0
Share

If you're getting the null pointer exception in the line of the debug, the only thing that can be is that _spawnPosition is null. $$anonymous$$ake sure you give it a value.

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

20 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

Related Questions

Distribute terrain in zones 3 Answers

Follow and Instantiated Object 0 Answers

Multiple Cars not working 1 Answer

[SOLVED]: Bullets passing through platforms - Brackeys Tutorial 1 Answer

Instantiating a GameObject from scene that changes 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