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 danthai · Nov 11, 2015 at 05:54 PM · c#instantiatematerial

Seemingly random behaviour when Instantiating numerous Prefabs

Hi, I hope someone can help me here. Just starting out on a project (all new to me this). I've built a Ground script which has a public GameCube property. This I'm populating with my ground cube prefab. When I run the game it should build me a ground depending on the public dimensions I'm setting it to. However I'm seeing some strange behaviour whenever it tries to instantiate the final cube in the ground. See below :

alt text Some times missing material

alt text Some times missing and completely out of position.

Here is my code for instantiating the cubes :

 for (int y = 0; y < yBricks; y++) {
             for (int x = 0; x < xBricks; x++) {
                 Instantiate(brick);
                 brick.transform.position = new Vector3(x * 20, 0, y * 20);
                 brick.cubeID = count;
                 brick.setMaterial(Random.Range(0, 4), count);
                 count++;
             }
         }

And instide the Brick class setMaterial function :

 public void setMaterial(int rndVal, int cubeCount) {
         print ("Rnd " + rndVal + " Cube : " + cubeID);
         cubeRenderer.material = Instantiate(cubeMaterials[rndVal]);
     }

It's worth pointing out that the final cube is being passed correct values :

 Rnd 2 Cube : 15

The Brick script has a public Array of Materials which I'm dragging to the Prefab in the IDE.

Anyone have any ideas? Many thanks

screen-shot-2015-11-11-at-174139.png (276.6 kB)
screen-shot-2015-11-11-at-174328.png (278.6 kB)
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
Best Answer

Answer by Soos621 · Nov 11, 2015 at 06:57 PM

I don't think you need to instantiate the new render material if you already have a public array, just access the array and set the material to it,

cubeRenderer.material = cubeMaterials[rndVal];

however, I'm not positive why the Y position of the bricks would move out of place but here's how i would script it, maybe it'll make a difference,

GameObject newBrick = Instantiate(brick, new vector3(x*20, 0, y*20, Quaternion.identity); newBrick.GetCompoent().cubeID = count; newBrick.GetComponent().material = cubematerials[rndVal];

Comment
Add comment · Show 1 · 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 danthai · Nov 12, 2015 at 08:32 AM 0
Share

Thanks for the reply Soos621.

By instantiating the brick different didn't make any difference however by just referencing the material rather than instantiating a new one seems to have fixed the issue. Thanks a lot.

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

37 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

Related Questions

Instantiate screenshots from camera *Realtime* 0 Answers

*Solved* Loop instantiating objects causes my Unity Editor to crash 1 Answer

Instantiate particle system attached to object 1 Answer

How to instantiate bullets in multiple transforms 1 Answer

Destroy instatiate object on trigger enter / collision,destroy instantiate prefab on trigger enter 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