• 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
2
Question by saldavonschwartz · Apr 23, 2013 at 06:47 AM · c#monobehaviour

is there a monobehaviour method guaranteed to get called before I can call getter / setters for custom properties?

Basically, I have a script that goes along the lines of:

public class EdgeCasting : MonoBehaviour {

 private LineRenderer edgeRenderer;
 private Vector3 _edgeOrigin;
 private bool nextNodeCreationInProgress = false;
 
 public Vector3 edgeOrigin {
     get {
         return _edgeOrigin;
     }
     
     set {
         _edgeOrigin = value;
         edgeRenderer.SetPosition(0, _edgeOrigin);
     }
 }
 
 void Start() {
     edgeRenderer =  transform.Find("Edge").GetComponent();
 }

     ...

I noticed that if (from another object) I instantiate the prefab to which this script is attached and then try to set the edgeOrigin property, like so:

 newEdge = (EdgeCasting)((GameObject)Instantiate(Resources.Load("Edge"), transform.position, Quaternion.identity)).GetComponent<EdgeCasting>();
 newEdge.edgeOrigin = transform.position;


newEdge's edgeRenderer is not yet initialized / assigned by the time I try to set it indirectly thru the edgeOrigin property. I assumed Start() was a sort of monobehaviour "constructor" if you will and so it was guaranteed to be called before one was able to manipulate a script instance. I guess I was wrong. So, is there any way to make sure any private members are initialized before being able to start accessing properties in the script / class? I mean other than making the renderer a public property and manually assigning it before using edgeOrigin.

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 Bunny83 · Apr 23, 2013 at 07:36 AM 0
Share

If your LineRenderer is part of your prefab, you can make your edgeRenderer variable public (or add the SerializeField attribute) and link the LineRenderer in the inspector. That way the variable is set automatically when the object is Instantiated.

1 Reply

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

Answer by whydoidoit · Apr 23, 2013 at 07:24 AM

You need to use Awake() as if it were a parameterless constructor.

Note that Awake does not run on Prefabs until they are instantiated, but you can call functions on your scripts on Prefabs (a generally bad idea unless you have some specific purpose in mind).

Comment
Add comment · Show 6 · 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 saldavonschwartz · Apr 23, 2013 at 07:30 AM 0
Share

right, my guess was that Awake or Start would be "sort of" constructors.However, you say that Awake() does not get called on prefabs but at least through Resources.Instantiate() both Awake() and Start() are being called for each instantiation. Still, like my question states, if I instantiate and try to set the edgeOrigin property, the renderer is still null.. Like, my call to the accessor seems to happen immediately before Awake() and Start(). If after the first Update() I try to re-access or set the property, then the renderer is already set.

avatar image Bunny83 · Apr 23, 2013 at 07:33 AM 1
Share

Awake is called before the Instantiate method returns. Start is called the next frame before Update. So Awake is called before you have the chance to access your properties unless you access them from the class constructor itself...

avatar image saldavonschwartz · Apr 23, 2013 at 07:37 AM 0
Share

@Bunny83 yes I just tested this out myself, If in my above code I move the initialization of the renderer from Start() to Awake() it does work just fine. Now, why did @whydoidoit say Awake() is not run on prefabs? Like I said, it DOES run on a prefab instantiated through Instantiate()

avatar image Bunny83 · Apr 23, 2013 at 07:51 AM 0
Share

Don't mixup Prafabs with instances of Prefabs ;)

This is like a picture of a car can't drive, a real car can ;)

avatar image whydoidoit · Apr 23, 2013 at 09:10 AM 1
Share

Yeah I mean that a prefab (not an instance) appears to be a real "thing" - you can set variables on it and call methods - but it is not really alive - it's not connected to real Unity objects. You can use this to preinitialize a prefab before instantiating it (and then the prefabs Awake call would have any variables you'd set available as it ran).

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

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

WaitForSeconds/Yield problem 1 Answer

C# -- Build character unit from script 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