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 iTechix · Apr 04, 2015 at 02:05 PM · c#guiprefabsreferencing

Prefabs not remembering references scripts / gui elements

I'm in the process of creating a simple game as part of my college project. I've encountered an issue with prefabs and the fact that they don't seem to be remembering my references to scripts or gui elements (such as text). I have things such as pickups setup and test enemies that affect other scripts / gui elements when certain things happen such as an enemy being killed = the score increasing by 1. I'm not sure why this is happening as the objects I'm putting into the prefabs have the references / connections working fine.

How the script or GUI object is referenced in the code:

 public ShootGun gunScript;
 public Text aCounter;

Inspector view of the non-prefab object: http://i.imgur.com/qFxUC2H.png Inspector view of the prefab object: http://i.imgur.com/g6vsN3m.png

Any help would be greatly appreciated.

Comment
Add comment · Show 4
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 AlwaysSunny · Apr 04, 2015 at 09:00 AM 0
Share

Prefabs remember references to objects that are part of the prefab; all other references will be lost.

Circumventing this is a broad topic. I call it "scene management" or "reference management".

The basic idea is, there's always a "most convenient" way to pass references around. Sometimes the script which spawns the instance can know and hand-off references to the new instance - this is usually the most straightforward case, but it isn't always convenient. Convenience is king.

$$anonymous$$ore complex scenes and games require more structured management. $$anonymous$$y current project has a nice tiered structure with an all-knowing singleton (GS$$anonymous$$ for game state manager) at the top who holds access to sub-tiers. I can drill down to whatever reference I want from anywhere in the project. e.g.

 GS$$anonymous$$.Active$$anonymous$$ap.Entities.Agents.AnyRandomNearby( GS$$anonymous$$.Player.transform.position );
 
 GS$$anonymous$$.Audio$$anonymous$$anager.PlayRandomFoleyOfType( Foley.Footstep );
 
 GS$$anonymous$$.UI.Vendor.SellButton.GetChild(0).GetComponent<Text>().text = "whatever";

Again, this is a broad topic. Just remember, there's always a "most convenient" way to get the references you need.

avatar image iTechix · Apr 04, 2015 at 10:30 AM 0
Share

Is this the most effective way to do it then?

Just to clarify, you have GS$$anonymous$$ as the overall parent of everything else in the scene, and within that you have other tiers for other elements (e.g. the player, the UI, etc) ? Little confused, sorry.

avatar image AlwaysSunny · Apr 04, 2015 at 11:06 AM 0
Share

GS$$anonymous$$ isn't the parent of these objects, it's just a singleton which holds public references to them, which I expose as static properties. That's how I like to handle some aspects of reference management. This is something you'll want a bit later in your Unity career; don't sweat it until you need it, I guess.

Where it's convenient, I let the script responsible for spawning an object hand that new object the references it requires:

 GameObject go = Instantiate(prefab) as GameObject;
 go.GetComponent<$$anonymous$$yScript>().someVariable = someReferenceIHaveHere;

The "best" way to handle reference management is equal parts personal preference, convenience, and common sense.

avatar image iTechix · Apr 04, 2015 at 11:21 AM 0
Share

Okay, I think I understand what you're saying now. If, for example, I instantiate a prefab of an enemy for my game and it should have a reference to GUI text on my interface that will update the score displayed by 1 each time an enemy is killed, how would I go about adding the reference in (doing the drag and drop into the inspector view) when I instantiate or create a new prefab for it?

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Prefabs-based UI or dynamical UI in OnGUI() method?,What better UI based on Prefabs or on OnGUI() method 0 Answers

Prefabs-based UI or dynamical UI in OnGUI() method?,What better UI based on Prefabs or on OnGUI() method 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Why is networkbehaviour underlined in green? 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