Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Cyclops · Feb 08, 2010 at 02:10 AM · gameobjectprefabvariable

Are there restrictions on assigning GameObjects to Prefab script variables?

I ran into what seems to be a restriction on how I can assign existing GameObjects to public script variables. I didn't see anything about it in the documentation, it should work, but, doesn't...

I have two Prefab asset/models, called pf-Box and pf-Tube. I drag pf-Box onto my Scene, so it's now instantiated at development time (is that the opposite of runtime?), as aBox.

pf-Tube will be instantiated at runtime, and I want it to have a reference to aBox, so I create a script, called s-Tube, with a public variable, tmpObject, and add the script as a Component to the Prefab pf-Tube.

Then, in the Inspector window, I select the triangle dropdown box for tmpObject, and try to assign aBox to it - but it won't assign.

Looking at the dropdown box, it separates objects into two categories, Assets and Scene. I can assign things in Assets to my script variable - but I can not assign objects already existing on the Scene to my variable.

Is that correct? Are there restrictions on what objects can be assigned to a (not-yet-existing) script variable? Or is there some deep, dark configuration option that I don't have set right to allow it? :)

Note - I know there are other ways to reference existing GameObjects. But using a public script variable is a lot simpler, and seems like it should work.

John C>
"For all your days, prepare, and meet them ever alike;
When you are the anvil, bear - when the hammer, strike."

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

3 Replies

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

Answer by Eric5h5 · Feb 08, 2010 at 02:57 AM

You can't assign scene objects to prefabs, because prefabs must be universal and scene objects only exist in a single scene. Imagine being able to assign a scene object to a variable on a prefab, and then dragging the prefab into a different scene where the first scene object doesn't exist--it can't work.

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 Cyclops · Feb 08, 2010 at 04:04 PM 0
Share

Ah, that makes sense. While learning Unity, I hadn't really paid attention to the Scene versus project aspect, but now I get it. Thanks.

avatar image
0

Answer by Jaap Kreijkamp · Feb 08, 2010 at 07:16 AM

Indeed, what Eric5h5 is correct. There's one exception however. You can reference to gameobjects that are either children of the prefab or the prefab itself. Example.

Given the following Scene.

 + Player
 |
 + Enemy
   | 
   + Gun

and Enemy has a script:

var player : Transform;
var gun : Transform;

When you set player to Player and gun to Gun and make a prefab of Enemy. The value of gun will be kept, but player, referring to a gameobject not part of the prefab, will set to null if you instantiate the prefab.

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
avatar image
0

Answer by MakoBec · Oct 10, 2014 at 09:32 AM

you can place the prefab to the scene then assign the gameobject to the inspector of the placed prefab.

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

1 Person is following this question.

avatar image

Related Questions

Access variable from script on other gameobject. 1 Answer

Prefab problem, wont accept gameObject in var 3 Answers

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

Prefab (Project Folder) to a GameObject (on the Scene) 2 Answers

How can i access in a prefab a variable from a gameObject? 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