Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by zan49er · Jun 30, 2017 at 06:56 AM · unity 2dprefabsjson

loading prefab from path

I am working on a 2d game with very detailed modding support (which I know is silly for a unity game, but I don't care), and I am loading some things from JSON files, such as the size of the map, and what doors lead where. Because of this, I can use one variable, the map to load, to load literally any map with a JSON file attached... except the actual tiles in the map, which are in a prefab. I don't want to have to make hundreds of public GameObject variables for every map prefab, so is there a way I can just load one from a path?

Example: instead of:

public GameObject ForestPath2; Instantiate(ForestPath2, new Vector3(0, 0, 0), Quaternion.identity);

do:

String mapName = ForestPath2; Insantiate("Assets/Maps/prefabs/"+mapName ,new Vector3(0, 0, 0), Quaternion.identity);

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

Answer by Vicarian · Jul 01, 2017 at 01:03 AM

From your example, it seems like you need to load up a dynamic type at runtime based on certain criteria, so you'll likely need to look into Reflection, where you can perform the AddComponent generic call by supplying a type determined at runtime. It would be disingenuous for me to regurgitate Reflection knowledge here, as it's a powerful, and quite complex tool. Some study will definitely be required, and a good starting point is the MSDN.

Or, you could simply rethink your design and create a Weather superclass that handles the loading of the different types of weather. Either way would work, though the latter would require far less code.

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 zan49er · Jul 01, 2017 at 01:46 AM 0
Share

I'll look into whether a superclass would work, thanks! I still get an error for the first issue (prefabs), though.

I don't know if a superclass would work because the whole point of this is that most stuff can be modified by a text editor and an idiot - If you want to make fire rain, make a class for hot raindrops, and a class to spawn them and clouds, and call it through a trigger like a mission. A superclass would still need to have its subclasses called... that or I'm way too tired to be program$$anonymous$$g...

avatar image Vicarian zan49er · Jul 01, 2017 at 01:50 AM 0
Share

For that, the Assets folder is assumed, and you have to place things in a Resources folder under the Assets folder, which also is assumed. So, to instantiate a prefab, you'll have

 Type t = Instantiate(Resources.Load<Type>("pathWithout$$anonymous$$ingSlash"));

which works as long as the prefab has a Component of the type Type attached to it. Otherwise you can simply do GameObject for the type.

Say I have a folder called Prefabs in my Resources folder, and I want to load the SomeCoolPrefab prefab, and it's just a vanilla Transform with some tediously configured other components and the like which are built-in to Unity. I'd use GameObject, and the call would be:

 GameObject someCoolPrefab = Resources.Load("Prefabs/SomeCoolPrefab");
 GameObject someCoolPrefabInstance = Instantiate(someCoolPrefab);

You can do the above all on one line by using the form I used previously. Otherwise, if you want to instantiate a lot of the prefabs, you can do the above to hold the prefab in memory, and make as many copies of it you need. Alternatively, if you know the type of a component on the prefab, you can use that type, using the generic form I used first, and the path would be similar.

avatar image zan49er Vicarian · Jul 01, 2017 at 06:09 AM 0
Share

Thanks! The map loads, and since I can load objects, I can just create one that has all the scripts for each weather condition.

Edit: upon creating a weather object, I've come across a problem - it can't create other objects?

Second edit: Never $$anonymous$$d, I'm an idiot and constantly destroyed and replaced the object, removing its timer.

avatar image
0

Answer by zan49er · Jun 30, 2017 at 09:50 PM

I tried this:

Instantiate(Resources.Load("Assets/Maps/prefabs/" + mapName + ".prefab"), new Vector3(0, 0, 0), Quaternion.identity);

but it gives an error which is that the object I'm trying to instantiate is null.

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 zan49er · Jul 01, 2017 at 12:47 AM 0
Share

I also need to load a script, but this:

         gameObject.AddComponent("Assets/Scripts/weather/" + currentweather + "/" + currentweather + ".cs");

is depreciated, so I tried this:

 weatherClass = new type("Assets/Scripts/weather/" + currentweather + "/" + currentweather + ".cs");
         gameObject.AddComponent<weatherClass>();

but I can't create a type like that.

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

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

Architecture advice on how to cycle a prefab's characteristics at runtime? 0 Answers

[Unity2D] Adding Sound to a Prefab 0 Answers

Spawned Rocket(Clones) still here after scene swap. 1 Answer

POST JSON method does not work when project has been exported to WEBGL and upload to firebase,help please? 0 Answers

how to parse a json feed 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