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 heinrich · Jan 14, 2011 at 08:01 PM · editorinitialization

Play button does not initialize variables of gameObjects which are parameters of a component?

I do not know if it is a bug or feature:

  • Scene A has GameObject B with attached Component C.
  • Component C has a parameter GameObject D.
  • GameObject D is supplied as a Prefab.
  • GameObject D has a Component E.
  • Component C now calls an Init-Function in Component E.

    public void Init() {
    Debug.Log("Init:");
    if (!initDone) {
        Debug.Log("Init: *** Initializing ***");
        initDone = true;
    }           
    

    }

    The variable init is defined as this:

    private bool initDone = false;

    When I hit the Play-Button the first time, I see:

    Init: 
    Init: *** Initializing ***
    

    After pressing the Stop-Button and the Play-Button of the Unity-Editor again, I see only

     Init: 
    

    So it seems to me that there is no new object created or the initialization is not done again. What is the explanation? Am I missing a Unity concept at this point?

    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
    2
    Best Answer

    Answer by Jesse Anders · Jan 14, 2011 at 09:36 PM

    Are you calling the Init() function on the prefab, or on a clone instantiated from the prefab? (If the former, I'd guess you're modifying the prefab itself, which is why the initialization only occurs once.)

    Edit: Response to comment below:

    So if a Prefab is assigned as a parameter no new GameObject is created, but the Prefab itself is assigned ... I would not have expected that behaviour intuitively.

    Why would a new game object be created? The 'GameObject' field is a reference, so when you assign the prefab to it, you're just assigning a reference to that prefab. (Maybe it's the difference between reference and value semantics that's throwing you off, which is why you were expecting a copy of the object to be made.)

    Workaround would be: Create an GameObject from the Prefab in the scene and submit that GameObject as a parameter?

    Well, it's not a workaround, since what you're seeing is the correct (expected) behavior. But, the usual way to do this is:

    • Assign a prefab to a script field (e.g. a GameObject reference, just like you're doing)
    • Use the Instantiate() function to clone new game objects from the prefab as needed

    So no, you wouldn't drag the prefab into the scene and then assign the resulting game object to the script field, if that's what you're asking. Rather, you'd drag the prefab onto the script field, and then clone new game objects from that reference.

    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 heinrich · Jan 14, 2011 at 10:44 PM 0
    Share

    The Prefab (Game Object D) is assigned as a parameter in the Unity Inspector. It is not instantiated in the scene. So you are right - I'm modifying the prefab (proven by a look at the Inspector).

    So if a Prefab is assigned as a parameter no new GameObject is created, but the Prefab itself is assigned ... I would not have expected that behaviour intuitively.

    Workaround would be: Create an GameObject from the Prefab in the scene and submit that GameObject as a parameter?

    avatar image Jesse Anders · Jan 14, 2011 at 11:34 PM 0
    Share

    See my edit above.

    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

    No one has followed this question yet.

    Related Questions

    Initialising List array for use in a custom Editor 1 Answer

    Running legacy Unity 4.2.1 on legacy MacOS 10.8 in current days 0 Answers

    InitializeOnLoad: Gameobject being destroyed? 1 Answer

    Parent losing references to Child fields after duplication, copy/paste 0 Answers

    difficulty with InitializeOnLoad and Application.isPlaying 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