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 noonoox · Nov 15, 2014 at 12:27 AM · prefabprefabsresource.load

Resource.Load prefab via variable?

I'm slowly going insane (by Vaas' defineeeetion of insanity at least); I am making a game similar to Bit.Trip: Beat as a fun little side project, but this problem I can't seem to fix and the internet isn't helping!

I have a list that acts as a queue for when to spawn different types of entities, e being a string of one of those variables in the list, and I want to load them dynamically so I can avoid a ridiculous amount of if/else statements that I no doubt will have. this is the line of interest:

GameObject p = Resources.Load("Prefabs/" + e.z);

Whenever I try to Resources.Load with a variable (even if it's a string that I KNOW is right,) it will spit out an error of the prefab being null, but when I use

GameObject p = Resources.Load("Prefabs/Basic Pip");

'Basic Pip' being the name of the current test entity, it will work fine!

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

1 Reply

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

Answer by Jeff-Kesselman · Nov 15, 2014 at 12:28 AM

try this code instead

  string path = "Prefabs/" + e.z;
  Debug.Log("Load path is "+path);
  GameObject p = Resources.Load(path);

Chances are e.z isnt what you think it is.

Debugging is the art of identifying and testing every assumption you are making until you discover one that is false.

Comment
Add comment · Show 10 · 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 noonoox · Nov 15, 2014 at 12:35 AM 0
Share

Doesn't work; In tests, using

Debug.Log("Prefabs/" + e.z);

spits out "Prefabs/Basic Pip". If I directly use

Resource.Load("Prefabs/Basic Pip")

it works fine, but it seems that using any variable in Resource.Load results in it returning null.

...And therein lies my problem

avatar image Jeff-Kesselman · Nov 15, 2014 at 12:46 AM 0
Share

I suspect you have non printing characters in the variable string because I promise you if its the same exact value C# doesn't care about where it came from. There is no way the called code could possibly even know the value came from a variable v. a constant string.

Try this, now..

   //string path = "Prefabs/" + e.z;
   string path = "Prefabs/Basic Pip";
   Debug.Log("Load path is "+path);
   GameObject p = Resources.Load(path);

And print the path string you are really passing DONT reassmble it in the Debug.Log.

Doing the latter introduces an unproved assumption that they are assembled the same in both cases, and in debugging you want to avoid introducing new assumptions at all costs.

avatar image noonoox · Nov 15, 2014 at 01:25 AM 0
Share

strange... that works... I shall begin more testing!

avatar image Jeff-Kesselman · Nov 15, 2014 at 01:28 AM 0
Share

I would look closely at where the value of e.z is co$$anonymous$$g from. $$anonymous$$aybe you could show us that code? $$anonymous$$aybe there is a newline on the end.

You might want to print:

 Debug.Log(e.z+"!");


If the ! point prints on the next line then there is a newline at the end you don't want.

avatar image noonoox · Nov 15, 2014 at 01:32 AM 0
Share

What I'm doing is taking each line in a text file and splitting it at /n

The contents are: 5.0,0.0,Basic Pip 10.0,0.0,Basic Pip

then I split each line at the , into 2 floats and a string, e.z is the string

ie:

[5.0,0.0,Basic Pip\n 10.0,0.0,Basic Pip] the first is dealt with individually [5.0,0.0,Basic Pip] the three are separated into the float x, the float y, and the string z [5.0][0.0][Basic Pip]

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Whats the best way to populate a Dictionary with multiple Prefabs? 0 Answers

Unity not Instantiating Prefabs Properly 0 Answers

how do I assign a prefab's child to another prefab's script's public variable? 1 Answer

Ways to Avoid conflict between prefabs in source control 2 Answers

is having a Prefab reference inside a a Scriptable Object legit? 0 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