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 /
This question was closed Sep 10, 2013 at 07:45 AM by DeadKenny for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by DeadKenny · Sep 09, 2013 at 10:12 AM · c#instantiateresourceslink

How to link string to GameObject/Prefab, to load from?

Second time I am asking this question, first time no reaction so I tried to make it more clear.

So... I have this script(s) which creates a sort of shop, which you buy vehicle/parts from. When you walk up to the Object and click, a GUI opens up with turrets and such parts in it. Its only two turrets for now created 5 times at random when you start it up with icons and details. So now I need to link the string of each object(turrets in this case)to the Prefab in the Resources folder.

How to do that? Here is the code(s) for where the icons and names for the turrets get added into the UI.

 // This is where the turrets get their names and cameos from.
 
 private static TurretClass CreateTurCams(){
 
 
 
 TurretClass turretClass = new TurretClass();
 
 string[] turretNames = new string[2];
 turretNames[0] = "gc40turret"; 
 turretNames[1] = "gc120turret"; 
 
 turretClass.Name = turretNames[Random.Range(0, turretNames.Length)];
 
 turretClass.Cameo = (Texture2D)Resources.Load(TURRET_CAMEO_PATH + turretClass.Name);
 
 return turretClass;
 
 
 
 }
 
 //This is where it gets added into the list for the GUI.
 
 private void PopTurrets(int x){
  for(int cnt = 0; cnt < x; cnt++){
 
  baseList.Add(VehicleConstructor.CreateTurretCameos());
 
 }
 
 
 }


So yeah basically, I need to get those two turrets in the GUI of the shop linked to the actual Prefab it is meant for so I can load it into the game and instantiate.

I tried many ways but I end up making a big mess.

Please help me... Thanks.

Comment
Add comment · Show 1
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 ShadoX · Sep 09, 2013 at 11:00 AM 1
Share

Have you had a look at http://docs.unity3d.com/Documentation/ScriptReference/Resources.Load.html ?

It looks like all you need is the prefab name and you can instantiate it with

 String prefabName = "turret";
 var instance : GameObject = Instantiate(Resources.Load(prefabName, GameObject));

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by ArkaneX · Sep 09, 2013 at 10:45 AM

If I understand correctly, you want to tie turret names (like gc40turret, gc120turret) to some prefabs in your Resources dir (eg. Turret40, Turret120). If yes, then you can do it the following ways:

1) Make the turrent names the same as prefab names, and then load them using Resources.Load (just like you do for texture).

2) Make mappings between names and prefabs using Dictionary class, initialize it in the Awake method, and use when accessing prefab:

 private Dictionary<string, GameObject> _dicTurretPrefabsByName = null;
 
 void Awake()
 {
     _dicTurretPrefabsByName = new Dictionary<string, GameObject>(); // <- UPDATE!!!
     _dicTurretPrefabsByName.Add("gc40turret", (GameObject)Resources.Load("Turret40"));
     _dicTurretPrefabsByName.Add("gc120turret", (GameObject)Resources.Load("Turret120"));
 }
 
 public void CreateTurret(string name)
 {
     var prefab = _dicTurretPrefabsByName[name];
     Instantiate(prefab);
 }
Comment
Add comment · Show 21 · 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 DeadKenny · Sep 09, 2013 at 12:09 PM 0
Share

Ah ok I am testing now... One thing though is there a way to automatically add the code to awake for any Prefab I put there?

So I don't have to put an awake code each time I make a new turret?

avatar image ArkaneX · Sep 09, 2013 at 12:16 PM 0
Share

I'm afraid it's not possible, because only you know what string you want to map to particular turret :)

But I guess that adding one line of code when creating new turret

 _dicTurretPrefabsByName.Add("newname", (GameObject)Resources.Load("NewTurretName"));

is not a big issue.

avatar image DeadKenny · Sep 09, 2013 at 12:50 PM 0
Share

Ah ok.

Having a little issue calling the function/void.

Nothing seems to be instantiating when I test at start(){}.

Resources folders are set, even the turret prefabs themselves are in their own Resources folder.

Names all check out too.

avatar image DeadKenny · Sep 09, 2013 at 01:01 PM 0
Share

How is supposed to be called? CreateTurret("gc40Turret");?

avatar image Jamora · Sep 09, 2013 at 01:02 PM 0
Share

The Start function needs to be capitalized. Just in case you tried with a lower case 's' like you wrote above...

Show more comments

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Distribute terrain in zones 3 Answers

How to break a prefab connection via c# 0 Answers

Multiple Cars not working 1 Answer

How to Instantiate from correct Icon. 1 Answer

how Load Prefabs after build 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