• 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 Bloodshot55 · Jan 03, 2017 at 01:58 PM · instantiate prefab

2 instantiation types and their benefits?

Ok, to start off with, im not a programmer, im a designer who is programming, so you'll have to excuse the possibility that this question may seem trivial or may have already been answered.

So i was basically running through my code trying to clear out any redundancies and change variable types (eg. int to byte) to reduce as much memory or cpu usage as possible.

For most things I have this (for example):

 //prefab assigned in unity editor
 public GameObject letterASymbol;
 
 //no prefab assigned in editor
 public GameObject[] alphabetSymbols = new GameObject [27];
 
 void update ()
 {
        alphabetSymbols [0] = (GameObject) Instantiate(letterASymbol, Vector3.zero, Quaternion.identy);
 }

but then i tried this (attempting to reduce number of GameObject variables)

 //All array elements assigned with prefabs in unity editor
 public GameObject[] alphabetSymbols = new GameObject [27];
     
     void update ()
     {
            alphabetSymbols [0] = (GameObject) Instantiate(alphabetSymbols [0], Vector3.zero, Quaternion.identy);
     }

It seems to work fine and even did a quick test to see what happens:

 //All array elements assigned with prefabs in unity editor
 public GameObject[] alphabetSymbols = new GameObject [27];
 
 bool onceTest = false;
 
 void Update () 
     {
         if (!onceTest)
         {
             alphabetSymbols [0] = (GameObject) Instantiate(alphabetSymbols [0], new Vector3.zero, Quaternion.identity);
 
             alphabetSymbols [0].GetComponent<SpriteRenderer> ().enabled = true; //as it was default set to false in editor by myself
 
             alphabetSymbols [0].GetComponent<SpriteRenderer> ().color = new Color (1f, 1f, 1f, 0.5f);
 
             Destroy (alphabetSymbols [0]);
 
             alphabetSymbols [0] = (GameObject) Instantiate(alphabetSymbols [0], new Vector3.zero, Quaternion.identity);
 
             onceTest = true;
         }    
     }

When its instantiated the second time, the alpha is still at 0.5f and stuff.

I guess my question is, if you're careful about making sure all the parameters are reset (colour alpha, position, scale, etc) before you destroy it (if thats what you want of course), is this better memory/cpu wise? Or am I quibbling about negligible differences? Is this a better way in terms of keeping code less cluttered?

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
0

Answer by Owen-Reynolds · Jan 03, 2017 at 06:28 PM

Like you wrote, you're not a coder. Imagine the mess a novice 3D modeller would make trying to "improve" an existing model for size/speed.

The main thing experienced coders know is not to worry about the time/space for a mere 27 objects. You'll save way more looking for far-too-large textures (for example.) (My super-long explanation is at taxesforcatses-dot-com in the C# section under "efficiency.")

RE: resetting before destroying

Destroyed objects are shredded back down to bits. That's a standard coding thing. Instantiate won't try to find a suitable old object (like searching a junkyard for a usable muffler.) It just grabs the first free bits. But people do use that "reset for reuse idea." You can not Destroy the object - just hide it and reuse it later. The standard name for that extra coding is a pool. But it's rarely worth it.

And spawning alphabetSymbols[0] over and over in Update? There's a Unity example of spawning blocks into an array (https://docs.unity3d.com/Manual/InstantiatingPrefabs.html). That seems like what you're trying to do.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Instantiated prefabs all stacking on top of each other 1 Answer

Instantiate and launch prefab that's overlapping player? 1 Answer

How can you find the index or a variable associated to said index, from a script run from that instance of the game object? 0 Answers

Rotation not working properly when ,Script rotation not working when instansiating. 0 Answers

How to load different content from database when select different option? 0 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