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 Reaper1121 · Mar 17, 2015 at 04:15 PM · c#assetcomponent

Get .prefab (object from unity) with component from assets.

Hello. I have a object with script components on it and put in Assets/Ingame Models folder. And i need to access his scripts in another script on object that is in unity not in assets folder. How can i do that? here is what i tried to do i found it on the internet:

 enemyinfo = Resources.LoadAssetAtPath ("Assets/Ingame Models/Enemy.prefab", typeof(GameObject)).GetComponent<EnemyHealth> ();

But getcomponent is red that means it does not work. How do i access it then? i am using unity 5 beta pro.

And gameobject.find does not work its only for ingame objects.

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 Neamtzu · Mar 17, 2015 at 04:28 PM 0
Share

Resource.Load will work only if your prefab is under Resources folder. If you don't have a Resources folder, create one, name it Resources and move the prefabs there.

 var enemyInfo = Resources.Load("$$anonymous$$odels/Enemy") as GameObject;
 var script = enemyInfo.GetComponent<YourScript>();

Note that you don't have to add the extension after the prefab name.

avatar image tigertrussell · Mar 17, 2015 at 04:31 PM 0
Share

Neamtzu is correct; if you are loading an asset using Resources.LoadAssetAtPath your files need to live at Assets/Resources/ and you must exclude the file name.

However, if you ins$$anonymous$$d create a public GameObject or public Transform property as written below, you can simply drag the desired GameObject into the script which is spawning new instances. I$$anonymous$$HO this is better, because the Assets/Resources folder is always included in your project, regardless of if items in it are referenced or not.

1 Reply

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

Answer by tigertrussell · Mar 17, 2015 at 04:21 PM

You need to create a Prefab, which you can do by right-clicking in the folder that you want, hovering over Create and selecting Prefab. Then, drag-and-drop the GameObject from your scene directly onto this new Prefab and give it a name.

Then check out the Instantiate documentation to see how to create an instance of that Prefab. So long as your Prefab has a Transform component, you can use a public Transform property of a class and drag the Prefab directly from the file browser onto the property of the script which will be spawning your prefab, and do something like on the example page:

 using UnityEngine;
 using System.Collections;
 
 public class ExampleClass : MonoBehaviour {
     public Transform prefab;
     void Example() {
         int i = 0;
         while (i < 10) {
             Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity) as Transform;
             i++;
         }
     }
 }


Comment
Add comment · 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

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

"Save As..." Saving level assets at Runtime 0 Answers

Low FPS in android game if using MK glow free asset 0 Answers

Create 3d hexagonal terrain 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