Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 DevLurkin · Oct 04, 2014 at 07:07 PM · instantiateprefabsharedmaterial

Instantiate a prefab and clone a cube in the prefab hundreds of times = 5 draw calls. Do the same thing again = thousands of draw calls?

Hey!

I have a GameObject prefab (Empty except for some scripts) that has a single child object (a cube, also a prefab).

I can instantiate the main prefab in my level and then instantiate the cube hundreds of times and I get 5 draw calls.

However, if I repeat the same exact thing, I end up getting thousands of drawcalls (one for each cube). Any ideas?

       GameObject tempCubeset = ( GameObject ) Instantiate( cubeSetPrefab , new Vector3( 0.5f , -11.5f , 0.5f ) , Quaternion.Euler( 0 , 0 , 0 ) );
       tempCubeset.transform.GetChild( 0 ).gameObject.renderer.material = new Material( Shader.Find( "Transparent/Diffuse" ) );
 
       for ( int i = 0 ; i < 500 ; i++ )
       {
           tempCubeset.GetComponent<CubeSet>().AddCube( new Vector3(0.5f, -11.5f, i+1.5f) );
       }
 
 
       //repeat (this forces a draw call per cube?)
       tempCubeset = ( GameObject ) Instantiate( cubeSetPrefab , new Vector3( 2.5f , -11.5f , 0.5f ) , Quaternion.Euler( 0 , 0 , 0 ) );
       tempCubeset.transform.GetChild( 0 ).gameObject.renderer.material = new Material( Shader.Find( "Transparent/Diffuse" ) );
 
       for ( int i = 0 ; i < 500 ; i++ )
       {
           tempCubeset.GetComponent<CubeSet>().AddCube( new Vector3( 2.5f , -11.5f , i + 1.5f ) );
       }

Any ideas what is causing this?

For context: I want to dynamically create "layers" of these cubeSets and be able to have several cubeSets each containing several cube children of their own. I am looking for a way to change the material/color of all cubes inside of a given cubeSet at runtime without having to step through every child individually. I am trying to use renderer.sharedMaterial to do this which seems to work great with one cubeSet, but not two?

Comment
Add comment · Show 2
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 tanoshimi · Oct 04, 2014 at 07:12 PM 0
Share

Can you show us the code from the AddCube function?

avatar image DevLurkin · Oct 04, 2014 at 10:02 PM 0
Share

Sure, the AddCube...

     public void AddCube( Vector3 cubeCoord )
     {
         GameObject cubeSection = (GameObject)Instantiate(baseCube, cubeCoord, Quaternion.Euler(0, 0, 0));
        cubeSection.transform.parent = transform;
     }

Edit: the parent transform pushes the cube into the empty cubeSet object.

1 Reply

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

Answer by DevLurkin · Oct 04, 2014 at 10:27 PM

I just figured it out.

It was being caused by the Shader "Transparent/Diffuse". When I created one cubeSet there was no overlap so things were fine. Adding the second just two units higher caused the transparency in the cubes to give me grief.

I'm guessing that a transparent cube needs its own draw call if there is a material behind it (at least it makes sense to me). Changing over to a plain "Diffuse" fixed my draw call problem.

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

2 People are following this question.

avatar image avatar image

Related Questions

How to make an Instantiated prefab a Child of a gameobject that already exists in the hierarchy 3 Answers

How to randomly instantiate other prefabs parallel? 1 Answer

What can prevent a prefab network instantiation? 0 Answers

my object keeps cloning is self i want it to clone once 2 Answers

Instantiate PreFab with C# Script 5 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