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 submodular · Oct 01, 2013 at 07:49 PM · instantiatebestpractices

Creating an object and setting fields before object begins running

Hi folks,

I have what may be a simple "best practices" question that I haven't seen a good answer for. Suppose I have ship objects and bullet objects, which are created by ship objects. I'd like each bullet to know the identity of the ship that created it. I've yet to find a satisfying way of doing this. Here are the things I've tried:

A) I had the ship create the bullet game object, and then immediately set the owner field in the bullet's script to self. The problem is, as soon as I give the bullet object a script, that script starts running, so I have no way to ensure that the bullet's owner field it set by the ship before the bullet script tries to access that field itself.

B) I had the bullet's Start function wait for the owner field to be set, but this seems clumsy.

C) I could have the bullet try to set the owner field itself, but (i) this would require the bullet to somehow know which ship it was created by and (ii), even if that weren't an issue, using Find feels similarly clumsy -- the ship just created the bullet, shouldn't the ship be able to pass along other information?

Thanks in advance for any suggestions on how to handle this more gracefully.

-sub

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
1

Answer by robertbu · Oct 01, 2013 at 09:05 PM

Do the initialization in Start() not Awake(). For example, create a prefab and put this script on a prefab:

 #pragma strict
 
 var id = -1;
 
 function Awake() {
     Debug.Log("Awake: "+id);
 }
  
 function Start () 
 {
     Debug.Log("Start: "+id);
 }

Then put this script on an empty game object with 'prefab' initialized to the prefab you created above.

 #pragma strict
 
 var prefab : GameObject;
 
 function Update() {
 
     if (Input.GetKeyDown(KeyCode.Space)) {
         var go = Instantiate(prefab) as GameObject;
         go.GetComponent(Test).id = 1;
     }
 }

When you hit the spacebar, you will see is that the value of 'id' will be -1 in Awake() but 1 in Start(). If this is not what you are experiencing, perhaps you want to post your scripts.

Comment
Add comment · 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

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

14 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

Related Questions

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

Split a cube into several pieces? 1 Answer

scripting question 3 Answers

Prefab GameObject's can not be made active! 3 Answers

Is there a way to call up a IF statement like a function? 3 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