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 Myhijim · Jun 16, 2012 at 07:35 AM · transformprefabvariableapplying

Applying a Prefab to a Variable

Hi all, this is most probably an easy fix, but I have been looking for it for quite a while.

How do I assign a prefab to a variable without running it through the inspector?

For example I want to create a system that is like the following

    var Weapon;
 switch(Weapon){
 case ("Large Stick"): Weapon = prefab("Large Stick"); break;
 case ("Candycane"): Weapon = prefab("Candycane"); break;
 case ("Lazer"): Weapon = prefab("Lazer"); break;  
 }

This is sorta the idea I'm looking for and i know the above script wont work, I am just asking how it is done.

Thanks Myhijim

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 syclamoth · Jun 16, 2012 at 07:49 AM 1
Share

Better question is: why? There is almost certainly a better way. String literals are always danger signals- if you have a solution that requires them, consider rethinking what you are doing.

avatar image Myhijim · Jun 16, 2012 at 01:58 PM 0
Share

I'm asking how to apply a prefab to a variable via its name, because I do not want hundreds of Transform variables in th inspector

1 Reply

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

Answer by Wolfram · Jun 16, 2012 at 02:07 PM

Well, you'll have to reference your objects somewhere - whether you type the 100 names in your script, or just drag the 100 prefabs into an array, it's not much of a difference, and the latter will give you much less hassle when dealing with your objects (although it will be more problematic if you later need to insert/delete objects from that array. Appending at the end is no problem)

One other method would be to put all your prefabs into a folder named "Resources". Then you can use Resources.Load("name") et al. to refer to these objects and instantiate them. However, note that the "Resources" folder will be included in all your builds, so the larger it gets, the larger your application will become, and unused objects in there will waste file size space of your application.

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 Bunny83 · Jun 16, 2012 at 02:26 PM 1
Share

Exactly! Usually you can just declare a Transform or Gameobject array and assign your prefabs to them.

 var prefabs : Transform[]; // assign your prefabs in the inspector.
 // [...]
 
 prefab[4] // the 5th prefab in the array

If you really want to handle it via a Resources folder and Resources.Load, your switch it totally useless if the switch statement is a string containing the exact asset name.

 var weaponName : String;
 var weaponPrefab : Transform;
 // [...]
 weaponPrefab = Resources.Load(weaponName);

Also you should use meaningful variable names and you should type them. dynamically types variables are slow and can give you strange errors at runtime since you and the compiler never know that's in the variable.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I set a Transform Variable in a Prefab? 1 Answer

How can I modify a variable on an instance of a prefab after I created it? 2 Answers

How to delete a Variable in a script in game 1 Answer

Bullet Prefab spawning in multiple random positions and rarely the correct one (Unity 3D) 1 Answer

rot not working for 2nd prefab 2 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