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 glasstongue · Jun 04, 2014 at 04:23 AM · instantiateloadingresource.load

Can I use a string to point to the Resources directory?

Is it possible to use a string to point to the location of a prefab in the Resources directory?

I'm writing a template that defines the stats and type of enemy that will show up when my game runs so I can change out the stats and enemy thumbnail when it runs.

Here is an example of how I'm hoping it can work:

 public static string monsterPrefab = "Prefabs/monsters/monster_000";

 void monsterImage () 
 {        
     GameObject myMon = (GameObject)Instantiate(Resources.Load(monsterPrefab));

 myMon.transform.position = new Vector3 (-2.097f, -3.53f, 50f);
     
 } 


It throws this error:

ArgumentException: The thing you want to instantiate is null. UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message) UnityEngine.Object.Instantiate (UnityEngine.Object original) spawnMultiObject.monsterImage () (at /Users/glasstongue/new unity project/Assets/scripts/gameScripts/spawnMultiObject.cs:383) spawnMultiObject.Start () (at /Users/glasstongue/new unity project/Assets/scripts/gameScripts/spawnMultiObject.cs:121)

Is it possible to call Resources.Load like this?

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

Answer by rutter · Jun 04, 2014 at 05:24 AM

The exact thing you're trying to do won't work, but there are other options that are very similar (and will work!).

You can use a string to refer to any asset in a Resources folder. See scripting manual.

If you prefer, you could have a public variable, set in the inspector, which refers to a prefab that you'd like to spawn.

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 glasstongue · Jun 04, 2014 at 06:16 AM 0
Share

Thanks rutter.

avatar image glasstongue · Jun 04, 2014 at 07:43 AM 0
Share

Actually, I got it working - it was a missing Collections.Generic in my template file that finally did it.

Despite me being the reason I'm pulling all my hair out - Unity is really an awesome tool.

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

22 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

Related Questions

How to save/load the Y-Coordinates of Instantiated Objects 1 Answer

How to load all necessary assets when in the loading screen 1 Answer

Checking if object intersects? 1 Answer

Why are my cached (in an array) gameobjects disappearing? 0 Answers

how to instantiate prefab with Resources.Load ? 4 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