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
0
Question by balauru · Jul 26, 2013 at 12:03 PM · instantiateprefabstartlink

Link variables of a script in dynamically instantiated prefab before start is called

It's actually 2 questions. What's the best way to set links of a script of an instantiated prefab, and is there any way to do that before the instantiated prefab's script calls it's start function?

I wat to set a bool. my start function looks something like this

 void Start ()
 {
     if(boolValue)
     {
     //do stuff
     }
     else
     {
     //do other stuff
     }
 }
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
0
Best Answer

Answer by amphoterik · Jul 26, 2013 at 12:22 PM

Use the Awake() function. It is just like start, accept it runs before start:

 void Awake()
 {
     //do what you need to do here.
 }

If you need to be able to pass a variable in to the method in question I would instead not use either the Start or Awake methods. Instead I would create a new method and put all my startup stuff in there. Then just have whatever script instantiates the object call that method. It would look like:

 //object script. Assume is it named "MyScript"
 public void Init(bool value)
 {
     //complete all if your startup stuff here based on the parameters
 }

Then in the script that creates the object:

 GameObject myObj = (GameObject)Instantiate(...);
 myObj.GetComponent<MyScript>().Init(true); // or false

I am 99.9% sure the setup will get called before the object's update. I believe the created object will queue and begin firing off in the next round of updates.

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 balauru · Jul 26, 2013 at 12:25 PM 0
Share

I taught about that, but i can't give it a parameter. Or is there any way to give it a parameter?

avatar image amphoterik · Jul 26, 2013 at 12:40 PM 1
Share

Oh, I see now what you are trying to do. I would ins$$anonymous$$d not use either the Start or Awake methods. Ins$$anonymous$$d I would create a new method and put all my startup stuff in there. Then just have whatever script instantiates the object call that method. How does that sound?

avatar image Bunny83 · Jul 26, 2013 at 12:42 PM 1
Share

That sounds like you should add it to your answer ;)

avatar image amphoterik · Jul 26, 2013 at 12:49 PM 0
Share

O$$anonymous$$, I did.

avatar image balauru · Jul 26, 2013 at 12:49 PM 0
Share

that sounds good. So i should move all the stuff from my start function in an Setup function, then add a new function like SetParameters and then when i instantiate i should do something like: Instantiate(); SetParameters(); Setup();

do i know for sure that Update() won't be called before if i want to use stuff i initialize in SetParameters inside Update()? Just realized it doesn't matter if update is called before becouse i can check if the variables are initialized or not.

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.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Start() of Instantiated Prefabs Not Being Carried Out? 1 Answer

Start() is called 2 times when Instantiating prefab 2 Answers

Trouble destroying a instantiated Prefab? 1 Answer

How to break a prefab connection via c# 0 Answers

How do I instantiate a projectile along the path of a raycast? 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