Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by TPRammus · Aug 15, 2017 at 01:18 PM · gameobjectinstantiatescript.gameobjects

Is it safe to instantiate a GameObject and then change it's components?

I have the fear that the object will be created and then "cause harm to its surroundings" before the code that changes it's position (for example) is being executed.

So for example I have an enemy that kills everything it touches (because of his high damage) in the OnTriggerEnter function so I dont want the OnTriggerEnter code to be executed before I changed all the necessary variables (for example the damage it inflicts or the position or whatever).

So will the GameObject be created right when the line Instantiate(); is being executed or will that happen after the end of the current function? (Im using C# if that matters)

regards, TPRammus

Comment
Add comment · Show 5
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 Necrodoc · Aug 15, 2017 at 03:26 PM 0
Share

Waiting for someone to answer as i would like to know that as well :)

avatar image Owen-Reynolds · Aug 15, 2017 at 04:06 PM 1
Share

Read the manual for examples of your top Q (Is is safe to ... .) Clearly the object is created right away, or those examples wouldn't work. But for your other Q, look at the sections on ti$$anonymous$$g for OnTriggerEnter. If you need do, try a test (Instantiate something inside a trigger, immediately destroy the collider, see if OTE fires.)

avatar image TPRammus Owen-Reynolds · Aug 15, 2017 at 04:12 PM 0
Share

Hmm thank you I will try that. What about the answer to this question? They seem to accept the answer but I dont really see any difference when I compare it to the code example of the questioner.

avatar image Owen-Reynolds TPRammus · Aug 15, 2017 at 04:43 PM 1
Share

Hmm...all the green reply for that Q shows is being able to use GetComponent after instantiating something. But that's already in the manual (and was when the Q was asked.) AND you're allowed to skip that line by directly instantiating by component (ex: public Rigidbody r1; ... Rigidbody r=Instatiate(r1);, which is also in the manual.

But it's the internet. You can get 11 people somewhere out there to click on anything.

Show more comments

1 Reply

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

Answer by SolomonG · Aug 15, 2017 at 04:11 PM

I don't believe that the components would be guaranteed to be changed instantly without affecting the world. If I were you, I'd have the prefab be disabled by default. Then, you instantiate it (still disabled) and change the components, or do whatever you want to do with it. Once you're done setting it up, simply set it to enabled, and all should work.

Comment
Add comment · Show 4 · 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 Owen-Reynolds · Aug 15, 2017 at 04:30 PM 0
Share

You should write a small test. Should take about 2 $$anonymous$$utes to Instantiate something with a collider overlapping a a physics object, then try disabling and/or destroying things and see how they react.

Very easy way to change your initial "don't believe" into "know," and might save you lots of lines in your scripts.

avatar image TPRammus Owen-Reynolds · Aug 15, 2017 at 05:21 PM 0
Share

Thank you I tried immediately setting the Transform.position to a new one, deleting the Collider and deactivating the GameObject. All three of those worked. (I instantiated inside FixedUpdate btw)

But I will still prefer this solution since here I can just enable the GameObject after I changed all of his settings. That gives me the safety that nothing goes wrong.

avatar image Owen-Reynolds TPRammus · Aug 15, 2017 at 09:29 PM 1
Share

Technically, you should check the correct answer to your Q, so it can help other people. Starting with the prefab disabled is just a red herring. But now you know why that other Q got a such a funny answer accepted. Plus this Q was moved into the Help Room area, where the rules are just do whatever seems best - nothing wrong with thanking someone (with a green background) for trying to help.

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

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

Related Questions

I need help about Basic Basketball Game 0 Answers

Destroy(this.gameObject) destroys all the obejcts in the scene, i want only the one that is clicked on 1 Answer

Instantiating GameObject and Adding to list 3 Answers

Destroy Instantiate is not working 0 Answers

Instantiated bullet wont destroy 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