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
1
Question by PaxForce · Mar 11, 2014 at 11:04 AM · inspectordynamic variables

how assign prefab from assets to variable

This is my variable to which I can assign one of my 20 prefabs from assets, in order to play a specific animation.

 public GameObject _spell;

I would like to assign via script a certain animation. For example, if my player picks up a "spell_Fire" PickUp, I would like Unity to dynamically assign the firespell prefab to my _spell variable.

Comment
Add comment · Show 6
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 thornekey · Mar 11, 2014 at 11:08 AM 2
Share

you could put them in a resource folder and load them in through that..

 Resource.Load
avatar image PaxForce · Mar 11, 2014 at 11:48 AM 0
Share

Thornekey - since I'm just beginning to code in Unity and C#, I would need a much more detailed help than that.

avatar image ShadoX · Mar 11, 2014 at 11:52 AM 0
Share

Have a look @ http://answers.unity3d.com/questions/24322/instantiate-prefab-wihtout-dragging-prefab-into-ga.html

avatar image thornekey · Mar 11, 2014 at 11:58 AM 1
Share

Ok, sorry. You can have folders called resource folders, and theyre never actually in your scene, just your assets and stuff. you can call them in scripts. so say if you wanted to spawn a mob..

     void Start() {
         GameObject instance = Instantiate(Resources.Load<GameObject>("$$anonymous$$r$$anonymous$$ob"));
     }

do you understand?

avatar image jacobschellenberg · Mar 13, 2014 at 06:43 PM 1
Share

You need a folder in your Assets folder called Resources. You than drop your spell prefabs into that folder.

Than from code you can say,

  GameObject instance = (GameObject)Instantiate((GameObject)Resources.Load("Spell_01"));
Show more comments

1 Reply

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

Answer by mclankyman · Mar 11, 2014 at 11:58 AM

You will still need a reference to the spell prefabs using the drag and drop method just so your script knows of them. But you can assign your variables a new type of spell or the different types by instantiating new ones.

Have a look at this documentation.

http://docs.unity3d.com/Documentation/Manual/InstantiatingPrefabs.html e.g

 public GameObject fireSpell;(assign prefab in inspector)
 ...
 MySpell = Instantiate(fireSpell, transform.position, transform.rotation);
Comment
Add comment · Show 7 · 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 thornekey · Mar 11, 2014 at 12:02 PM 2
Share

yes, actually i think this will work best for him.

avatar image PaxForce · Mar 13, 2014 at 03:27 PM 0
Share

mclankyman - you described how to do something I said I do not want to do. I would like the prefabs to load dynamically, meaning I do not want to assign a prefab manually to the inspector by dragging and dropping. I would like the Player1 to dynamically load FireSpell, when Player 1 walks over a FirePickUp.

avatar image mclankyman · Mar 13, 2014 at 08:08 PM 1
Share

Well the first answer is for you then. It should work when using resources. Have you definitely made it a prefab and not just a gameobject? Bare in $$anonymous$$d loading from resources is marginally slower than inspector drag and drop in terms of performance.

avatar image PaxForce · Mar 13, 2014 at 08:10 PM 0
Share

but I can't drag 20 different spells into 1 slot in the inspector, am I right? :)

avatar image mclankyman · Mar 14, 2014 at 12:24 PM 1
Share

No of course not. But you could drag and drop all 20 types of spells but have 100s of one type of spell.

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

23 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Define lerp axis through inspector 1 Answer

The parallel function to Selection.activeObject in the Inspector? 0 Answers

c# enum wont show in inspector 3 Answers

manually refresh scripts in inspector 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