Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Sam 4 · Feb 27, 2011 at 11:48 PM · instantiateprefabattribute

Is there a way to set an attribute of a Prefab pre-Instantiate()/tion?

I am trying to Instantiate a Prefab and manipulating an attribute before i call the Instantiate() function. This is because each Prefab I instantiate is meant to have a different behaviour type.

Please Help :)

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

3 Replies

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

Answer by Jesse Anders · Feb 27, 2011 at 11:55 PM

Typically you would make any necessary modifications to the instance immediately after instantiation. (You can use the return value of Instantiate() to access the instance.)

Comment
Add comment · Show 1 · 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 Sam 4 · Mar 28, 2011 at 11:25 PM 0
Share

I will look into that, Thanks Jesse

avatar image
1

Answer by Bunny83 · Feb 28, 2011 at 12:57 AM

Like Jesse said, you can access attached scripts after instantiate with GetComponent() or add new scripts with AddComponent(). Just keep in mind what events are fired first.

Two little test scripts. A gun and a bullet script. The bullet script is attached to the bullet prefab.

//gun var bulletPrefab : Transform; private var logNextUpdate : boolean = false; function Update () { if (logNextUpdate) { Debug.Log("Gun:next Update() after instantiate"); logNextUpdate = false; } if (Input.GetKeyDown(KeyCode.W)) { logNextUpdate = true; Debug.Log("Gun:Update()"); var BulletShot = Instantiate(bulletPrefab, transform.position, transform.rotation); Debug.Log("Gun:Update:Instantiate done"); } }

//bullet private var logFirstUpdate : boolean = true;

function Awake() { Debug.Log("Bullet:Awake"); } function OnEnable(){ Debug.Log("Bullet:OnEnable"); } function Start() { Debug.Log("Bullet:Start"); rigidbody.AddForce(transform.forward * 1000); } function Update () { if (logFirstUpdate) { Debug.Log("Bullet:Update first run"); logFirstUpdate = false; } }

You will get this order:

  1. Gun:Update()
  2. Bullet:Awake
  3. Bullet:OnEnable
  4. Gun:Update:Instantiate done
  5. Bullet:Start
  6. Gun:next Update() after instantiate
  7. Bullet:Update first run

As you can see Awake and OnEnable are triggered immediately when you instantiate your object. Use Start() if your object needs to do some initialization. That way you can easily setup some variables on the new created object before it initializes itself.

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
avatar image
0

Answer by AngryOldMan · Feb 28, 2011 at 12:55 AM

you could possibly make several prefabs with the different options already set and include an array for the different prefabs in the spawner script

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

No one has followed this question yet.

Related Questions

Don't render prefab until onCollision - related, parent prefab.. 0 Answers

Why is transform.position of a prefab that's been instantiated different than a gameobject created on the scene, when they are in the exact same position? 1 Answer

Instantiating prefab with certain coordinates problem (c#) 0 Answers

Instantiate prefabs just before it comes into view 2 Answers

Access a function of an instantiated prefab 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