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 Carbongrip · Feb 23, 2014 at 03:28 AM · c#gameobjectnullblocksresource.load

Setting Prefab with Code Returns NULL

Ok I am going crazy how come none of these work? I am trying to make a the GameObject prefab to set to a prefab that I want. So I can later place that block.

prefab = (GameObject)Resources.Load("Objects/Blocks/Dirt") as GameObject;

prefab = (GameObject)GameObject.Instantiate((GameObject)Resources.Load("Objects/Blocks/Snow"));

prefab = Instantiate(Resources.Load("Resources/Blocks/Dirt", typeof(GameObject))) as GameObject;

All return null!

After doing this I then do Instantiate(prefab, G, Quaternion.identity); which works fine but only does it when I drag object to prefab in editor. This can't happen since I need to switch the prefab to other types of blocks.

Comment
Add comment · Show 2
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 whydoidoit · Feb 23, 2014 at 06:02 AM 0
Share

So what is Dirt? Whatever it is, it's in Resources/Objects/Blocks right or something that looks like that?

avatar image Carbongrip · Feb 23, 2014 at 08:54 AM 0
Share

Dirt is a Dirt block prefab that is in a folder under Assets/Objects/Blocks

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by RudyTheDev · Feb 23, 2014 at 04:27 PM

Because Resources.Load() very specifically says your files have to be in "Resources/" folder.

Also, any reason why are you loading them from resources rather than having a reference to your prefabs from the script? Is it only ever used once?

Comment
Add comment · Show 5 · 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 Carbongrip · Feb 23, 2014 at 06:23 PM 0
Share

Its used more than once and I have to be able to switch the type of block i can place on the fly. So I will make a Resources folder and try it.

avatar image whydoidoit · Feb 23, 2014 at 06:32 PM 0
Share

I hate Resources personally - would suggest an array of prefab references in an object in your scene.

avatar image Carbongrip · Feb 23, 2014 at 06:37 PM 0
Share

Thanks so much didn't see that in the unity docs thank you. If anyone wants to know this is the code the worked.

 prefab = Instantiate(Resources.Load("Dirt", typeof(GameObject))) as GameObject;

Don't forget to place them in a folder you create called Resources :)

avatar image RudyTheDev · Feb 23, 2014 at 06:50 PM 0
Share

@whydoidoit: me too! That's what I always do myself -- make a prefab manager singleton class and keep all my prefab references there. And using Resources a lot is definitely slower.

avatar image whydoidoit · Feb 23, 2014 at 06:51 PM 0
Share

And you know your build isn't bloated with a bunch of things you forgot in the Resources folder

avatar image
0

Answer by joseques · Feb 23, 2014 at 06:47 AM

Why you dont try to make a List ?

 public List<GameObject> mylist;
 prefab = (GameObject)Instantiate(mylist[index], G, Quaternion.identity);
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 Carbongrip · Feb 23, 2014 at 08:12 AM 0
Share

Im trying to load different resources based on a set of variables not keep track of each block.

avatar image joseques · Feb 23, 2014 at 06:07 PM 0
Share

Why are voting negative on my answer? I just only want to help him, I've believed that List could help him...

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

21 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

Related Questions

Simple Looping drag and drop game 1 Answer

Gameobject as Optional Argument 0 Answers

Distribute terrain in zones 3 Answers

I have an error, script should check for null or not destroy game objects 2 Answers

Null Reference Exception For Blocks C# 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