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
Waiting for someone to answer as i would like to know that as well :)
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.)
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.
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.
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.
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.
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.
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.
Your answer
Follow this Question
Related Questions
I need help about Basic Basketball Game 0 Answers
Instantiating GameObject and Adding to list 3 Answers
Destroy Instantiate is not working 0 Answers
Instantiated bullet wont destroy 1 Answer