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 thebobking · Jul 12, 2013 at 09:59 PM · prefabinspectorgetcomponentpublic variableanother script

Assign object in hierarchy as public variable in prefab

I have an object that runs all of my game logic called GameController.

I want each instantiation of a certain prefab to be able to access that gameController.

In the script of the prefab, I have:

 public var variableGameController:GameObject;  


Here's the problem, though.

Experimentation has shown that if you drag the GameController object to the variableGameController in the inspector,

that information is not propagated in to instances of the prefab. In the prefab instances, the field just remains blank.

Why? What to do?

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

1 Reply

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

Answer by robertbu · Jul 12, 2013 at 10:27 PM

A prefab is a plan for a game object, not a game object. You can drag and drop variables between game object internally, but you cannot and drop externally. That is, you can create internal linkages between the scripts on the various parents and children of the prefab, but since since the prefab/plan does not know about it environment, you cannot make direct references beyond the prefab to other game objects.

There are a variety of solutions. The most common is to use GameObject.Find() or GameObject.FindWithTag() to find the the external link. This is usually done in the Start() function of any script in the prefab that needs to reference another gameobject/script. Another solution is to use a Singleton to provide global access to data.


The very easiest way... assuming you have only "one" of the thing in question ... just use FindObjectOfType ...

 /// here we are in a prefab...
 public SceneManager myBoss; /// won't work!
 private SceneManager myBoss; /// so do this...
 public void Awake()
     {
     myBoss = Object.FindObjectOfType<SceneManager>();
     }

(Be aware this is quite slow; do not do many of these at game time or in a loop at game ime. It's absolutely fine to do this at startup or scene startup.)

Comment
Add comment · Show 2 · 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 Fattie · Jan 21, 2016 at 07:25 PM 0
Share

I did not know that!

You can't make a Inspector variable link to a game object "outside" the prefab

cheers!

avatar image Starks72 · Jan 04, 2018 at 04:00 AM 0
Share

So, If I have a prefab that is instanced( and its variables are blank), I know I need to find the Transform and parent / child game Object that I want to set that is in the hierarchy. Also If I'm calling multiple prefabs from an array that are destroyed and called multiple times this would not work? Scene manager is slow. Right? I placed two images to help paint a clearer picture of what I am trying to say. I know the terms I may be using are in the wrong context. Also note that temp parent and carry space both should be assigned carryspace under player. alt text

alt text

screen-shot-2018-01-03-at-90210-pm.png (19.3 kB)
screen-shot-2018-01-03-at-85927-pm.png (10.5 kB)

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

18 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

Related Questions

When adding a script using GetComponent<>(), some prefabs that are assigned don't come up 1 Answer

Does not contain definition 1 Answer

I can modify the GameObject transform using the inspector, how can I modify the mesh as well? 0 Answers

Unity scriptable object inspector, field overlap eachother 0 Answers

Referencing variables via external script? 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