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 GrantFleming · Mar 24, 2017 at 11:07 PM · instantiateinstantiate prefab

question about operating on an instantiated object

Hi all,

I am working my way through some tutorials on working in unity with C# and I have a question about the following code:

 //Instantiate a new muzzleFlashPrefab object and store in the variable clone
 Transform clone = Instantiate (muzzleFlashPrefab, firePoint.position, firePoint.rotation) as Transform;
 
 // set the new objects parent to firepoint.
 clone.parent = firePoint;
 
 //create a random float and use this as the size of clone
 float size = Random.Range (0.3f, 0.6f);
 clone.localScale = new Vector3 (size, size, size);
 
 //Then destroy the muzzleFlash
 Destroy (clone.gameObject, 0.02f);

My question is this. Obviously the code randomises the size of the "muzzle flash" but it does so after the object has been instantiated. Now this codes works but I am wondering if anyone can tell me why? Surely if the object has been already been instantiated at default size this shouldn't do anything? Or am I instantiating it (so it appears on the screen) then changing the size so quickly I don't notice the original size before deleting it?

Apologies if this is a silly question I feel like I'm missing something here.

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
Best Answer

Answer by AlwaysSunny · Mar 24, 2017 at 11:06 PM

This is a valid question and an important concept to understand early. Two points worth making here:

Everything in computer science happens sequentially, and drawing to the screen is the last thing that occurs in a given frame. This is not the most technically accurate way to explain this idea, but it's true until you get into things like multi-threading or post-processing effects.

So you can create an object, and in the next few lines, change its size 5000 times in a loop, and what you'll actually see on the screen is the last change made. In other words, the five-thousandth change is the one drawn to the screen.

There's nothing wrong with making changes to an object (its prefab) before that object is instantiated, but it's generally not very intuitive to do that. Make your object, make your changes, then it's drawn to the screen at the end of the frame. Hope that clarifies things a bit!

Comment
Add comment · Show 3 · 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 GrantFleming · Mar 24, 2017 at 11:13 PM 0
Share

Ah of course! The method in which code is based is called on an object from the Update() function so once per frame. So everything in there is actioned before the frame is drawn in screen?

Thank you for explaining that I think I understand.

avatar image GrantFleming · Mar 24, 2017 at 11:15 PM 0
Share

and of course because the Destroy function has a time delay on it although it is written or called during the same frame it is actually not happening until many frames later?

avatar image AlwaysSunny · Mar 24, 2017 at 11:20 PM 0
Share

Yes and yes. Correct on all points. You've got it!

Best wishes,

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Assign variables on Instantiation? 0 Answers

Instantiated object not in right position 1 Answer

Instantiation Problem 0 Answers

Instantiated prefabs acting different than those dragged onto scene 0 Answers

Im having an issue when Instantiate Prefab with specific rotation 2 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