• 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 Cherno · May 12, 2014 at 06:26 PM · prefabunity serializer

[Unity Serializer] Prefab objects don't keep references of other objects

If a gameObject with Prefab identier is instantiated during runtime and has a script that keeps a reference to another gameObject, after loading, the reference is gone. Other things are kept, and it doesn’t matter if the reference is part of a custom class or whatever. As long as it’s a gameObject (and probably transform as well), it’s lost, but things like strings and ints are kept.

Does anyone have a solution?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Wiki

Answer by Kiwasi · May 12, 2014 at 07:40 PM

There are a number of ways to solve the problem, take your pick from solutions I have used below.

Attach a script containing the following

 GameObject myGameObject
 
 Awake {
     myGameObject = GameObject.Find("NameOfReference");
 }

There are other ways to do this as well. If you are looking for a manager you can declare a static variable on the manager as follows. This only works for GameObject you plan to keep only a single instance of.

 // On the manager
 public class MyManager: MonoBehaviour {
 
     public static GameObject myManager;
 
     Awake {
         myManager = this;
     }
 }
 
 ...
 
 // On the prefab
 GameObject manager;
 
 Awake {
     manager = MyManager.myManager;
 }

Finally you can set the reference from the instantiating script. This works best if you need to set a different reference each time.

 //On the calling object
 myClone = Instantiate(// Instantiate paramaters);
 myClone.publicReference = myGameObject;
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 Cherno · May 12, 2014 at 09:04 PM 0
Share

Okay, thanks for your suggestions. I'm not sure it's applicable for my case however; the situation at hand is as follows:

One "editor" Gameobject (no prefab) that can instantiate a "switch" gameobject, and select this switch to access the switch's triggers.

The editor can also assign another instantiated gameobject (say, a door) to a trigger variable of the switch. So we have three gameobjects interacting. One that is instantiating all prefabs, one trigger that holds references to trigger gameobjects, and one trigger gameobject like a door.

avatar image GodManOmar777 Cherno · Apr 08, 2018 at 09:50 PM 0
Share

by God$$anonymous$$anOmar777 · 1 $$anonymous$$ute ago

this doesn't work when you need references, i made a game with different characters you can select and every time i instantiate a character aka gameObject i cant get access to my moblie ui controller or none of things attach to original prefab like my slider which i use for health you can't access if you find a solution to this problem please give me a a haller

avatar image dgreedy · May 29, 2015 at 06:45 PM 0
Share

I've been running into this same problem a lot too. Anyone know why Unity was designed so that these references aren't automatically copied on instantiation?

avatar image
0

Answer by GodManOmar777 · Apr 08, 2018 at 09:46 PM

this doesn't work when you need references, i made a game with different characters you can select and every time i instantiate a character aka gameObject i cant get access to my moblie ui controller or none of things attach to original prefab like my slider which i use for health you can't access

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Saving customized transform in game 1 Answer

Rotating specific prefab - instantiated with Random.Range 2 Answers

Why Transform and not Prefab? 2 Answers

Unable to Instantiate prefab. Prefab may be broken 3 Answers

Array of prefabs 0 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