• 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 Jnz · Feb 24, 2017 at 09:50 AM · instantiateprefabs

Where to initialize prefab's properties?

Hi, i am developing a game where i dynamically create prefabs using Instantiate method:

 var go = Instantiate(PlayerPrefab, position, rotation);
 var player = go.GetComponent<Player>();
 // then i set some initial values for player, for example:
 player.color = red;

Note that i initialize some field values after Instantiate. In player script i need to read those values and initialize object (in this example it would be to find active material and set it's color to player.color).

I could use OnStart method to do this, but it's not reliable (sometimes it's not called before first Update). Okay, so i can use Awake? Nope, it is called at the Instantiate, when player.color is not yet set.

So what method should i use to initialize my material color? Or maybe unity doesn't have such method that is guaranteed to be called before first Update?

Comment
Add comment · Show 1
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 Jnz · Feb 24, 2017 at 01:18 PM 0
Share

OnRenderObject()

1 Reply

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

Answer by hrgchris · Feb 24, 2017 at 12:12 PM

Hi there

Start is probably the correct place. I'm not sure what you mean by 'not reliable'. Check out this diagram from the docs - it's very handy! Start is guarunteed to be called once for every object, and will be called before FixedUpdate or Update.

https://docs.unity3d.com/Manual/ExecutionOrder.html

OnEnable will (I believe) be called from within the call to Instantiate (note: worth checking - this is from memory), so your setting of player colour occur after OnEnable was called on the player component. Although I've not tried, Always will probably suffer the same issue.

-Chris

Comment
Add comment · Show 5 · 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 Jnz · Feb 24, 2017 at 12:46 PM 0
Share

You are right, Start is called before Update, maybe i poorly stated what problem i am getting exactly. But it is possible that Start will not be called before object is rendered by Unity when object is created through Instantiate. This is a problem because for one frame player will be rendered with default color, and only on the next frame it will change to red color. So far to me it seems that i need to create method Init(Color color) and call that after i call Instantiate. This would be fine except I want to be able to create game object using editor also, not only from C# code. In such case I would have to somewhere call this Init method (most probably on Start or Awake). But if i do that then when object is being created with Instantiate the Init method will be called twice. So am I missing something or unity doesn't have such a method that is called before object is being rendered for first time?

avatar image hrgchris Jnz · Feb 24, 2017 at 12:53 PM 0
Share

Hmmm. I've never seen Start fail to be called before render. Perhaps you're spawning things from within Render? Or maybe there's a bug? :)

I appreciate this isn't helpful, but can I just suggest adding Debug.Log to the various OnEnable, Start, Update and OnRenderObject function calls involved, just to be 100% certain that there isn't some little bug you've not picked up on.

If indeed you see OnRenderObject being called on your player before Start is called, that would be new to me - the whole point of start is to initialise stuff before the object does anything and I have used it for that extensively over the years.

avatar image Jnz hrgchris · Feb 24, 2017 at 12:58 PM 0
Share

Hmmm. $$anonymous$$aybe it is some other problem that is causing this (I assumed it was because OnStart was not called before first frame). I will try to investigate it further and will post what I have found.

Show more comments

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate Reference Problem 1 Answer

Fireball instantiate - lags 1 Answer

Prefabs wont instantiate using a for loop? 1 Answer

Instantiate sometimes creates more then 1 objects 1 Answer

instantiate in the editor (Prefab Problem) 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