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
5
Question by faultymoose · Apr 17, 2013 at 12:30 PM · prefabmonobehaviourinstantiationawake

Awake() not called on script referenced directly from Project window

I have a GameObject called TestObject in the HIERARCHY. It has a SimpleScript component.

 public class SimpleScript : MonoBehaviour
 {
     public GameScreen activeScreen;
 
     void Start()
     {
         activeScreen.Test();
     }
 }

The GameScreen component looks like this:

 public class GameScreen : MonoBehaviour
 {
     void Awake()
     {
         print("Awake called.");
     }
 
     public void Test()
     {
         print("Test Successful.");
     }
 }

In my PROJECT view, I have a prefab called SplashScreen which has a GameScreen added to it as a component. There's nothing else on it.

I drag the SplashScreen prefab onto the inspector for the TestObject's activeScreen field. When I run the game, the words "Test Successful" are printed to the output window, but the words "Awake called" are not.

It seems like a prefab dragged from the PROJECT view doesn't have Awake() called on it. But if I drag it to the HIERARCHY view first to create an instance of it, and then drag that instance onto the TestObject's activeScreen field, it works fine.

Is there some way to make Awake() call on the prefab dragged from the PROJECT view?

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 whydoidoit · Apr 17, 2013 at 12:32 PM

The system is behaving as designed.

When you dragged it from the project window onto a variable in the inspector you don't have an instance of it - you have the prefab reference (which obviously shouldn't wake up). You are expected to make an instance of that item to use it.

When you create an instance out of the prefab, the instance will have Awake() called on it.

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 faultymoose · Apr 17, 2013 at 01:56 PM 0
Share

Okay I understand that, but why does the method work if there's no instance?

avatar image whydoidoit · Apr 17, 2013 at 01:58 PM 1
Share

Because you are accessing the reference instance in the project. This is useful so that you can modify the object before instantiating a copy. ($$anonymous$$g. setting properties that would be used by Awake).

You shouldn't access these objects for any other reason however.

avatar image Bunny83 · Apr 17, 2013 at 02:54 PM 2
Share

Technically there is an instance in memory which is created when the game loads, but it's not part of the scene and game mechanics. It's just a "source object" for Instantiate which will create a clone in your actual scene. The pure C# / managed part of the object works, but the actual engine binding isn't there. Awake is a callback that is called from the engine itself. Only objects in the scene which are active and enabled are updated.

From Unity's point of view this object isn't really there.

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

13 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

Related Questions

Instantiating in Awake() v Setting up prefab instances in Editor performance difference? 1 Answer

Unity Loosing prefabs? 1 Answer

Animated Child 0 Answers

Prevent Additional GameObjects from instatiation 1 Answer

Component's Start/Awake methods not being called 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