Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 PsychoDuckArcade · Jan 28, 2015 at 12:02 AM · instantiateprefabruntimecomponentreference

Instantiating scripts with references to prefabs loses the reference.

So as far as I know, there are 2 ways to attach a prefab reference to a variable in a script:

1) Drag the prefab into the public variable through the inspector; Problem: The reference is lost when the script is instantiated at run time.

2) Load the prefab by string name with resources.load(); Problem: Renaming or moving the script requires you to edit the name or path of the string passed into resources.load() in every location that loads that prefab.

Is there really no way to reference a prefab without having either of these issues?

The best solution I've come up with is to use resources.load but instead of passing in the string directly, pass a string variable containing the string from one globally accessible location (so renaming/moving the prefab means I only have to change the name/path at one location in code). This means that for every gameobject i might want to instantiate at run time, I'd need to add a variable in global scope reference that object's prefab. While this is fine, it just seems a bit dirty, tedius, and unexpected to have to do in unity. I feel like there must naturally be a better way that I just don't know about.

My specific situation: I'm making an rpg. There are items in the form of scripts that are instantiated at run time. Some items need to instantiate gameobjects (like projectiles or art assets like a model of that item). So logically, they need a reference to that gameobject, which is a prefab.

Originally I manually dragged the prefabs the item needs to public variables in that item script through the inspector (which is an easy workflow). But when I instantiate the item at run time, the variables are cleared. The only time it doesn't clear and actually maintain it's value (the reference to the prefab) is when I drag the item into the scene in edit mode. But I need to add them to the scene at run time, not before hand in edit mode.

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 DanSuperGP · Jan 28, 2015 at 12:23 AM 0
Share

I'm not clear... why are you adding scripts on their own ins$$anonymous$$d of instantiating prefabs with the scripts already on them?

avatar image PsychoDuckArcade · Jan 28, 2015 at 12:42 AM 0
Share

Because the scripts aren't on the prefabs that they're spawning. They're on a different object doing the spawning through the script.

1 Reply

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

Answer by DanSuperGP · Jan 28, 2015 at 12:26 AM

One easy solution to this is to have a globally accessible manager object with a list of game objects...

Then you drag all the prefabs you need to have accessible at runtime into that list...

And you have a publicly accessible Dictionary

And then on start, you iterate through the list... and add each game object in the list to the dictionary, using it's name as the key...

Then you have a reference by name to all your prefabs... and you can get references to them by their name from the globally accessible dictionary.

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 PsychoDuckArcade · Jan 28, 2015 at 12:45 AM 0
Share

Yeah I thought of this, I'm just hoping there's a better solution. It seems odd Unity has all these features for simple workflow, yet to do something as simple as instantiate a script that instantiates prefabs requires us to make a manager and drag every gameobject spawnable at run time into it.

I figure there's a much simpler solution, something I'm missing or I'm doing wrong.

avatar image DanSuperGP · Jan 28, 2015 at 12:53 AM 0
Share

"I figure there's a much simpler solution, something I'm missing or I'm doing wrong."

No, I don't think so. I've been using Unity for 7 years.

Your options are...

$$anonymous$$ake some GameObject that has all the references to your prefabs, set it up in the inspector, then either have it in the scene to begin with or instantiate it from a prefab ins$$anonymous$$d of AddComponent.

Use Resource.Load

That's pretty much it.

avatar image PsychoDuckArcade · Jan 28, 2015 at 01:21 AM 0
Share

Alright then. Thanks.

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

Best practice for assigning class reference at runtime? 0 Answers

Reference handle on a prefab? 1 Answer

Instantiated Object turning off scripts and audio source?(Solved) 1 Answer

How to check if a component is on an instantiated prefab? 1 Answer

making prefabs at runtime? 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