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
1
Question by plsright · May 03, 2015 at 03:44 PM · javascriptarraymeshrenderer

Get MeshRenderer Component of Gameobjects in an Array

I'm trying to write a script in which the Mesh Renderer component is accessed from all of the game objects in an array. Here's what I've written so far:

 #pragma strict
 var limbs : GameObject[];
 
 function Start(){
     limbs = gameObject.GetComponents.<MeshRenderer>();
 }

I feel like I'm on the right track but I'm getting the error:

Assets/Scripts/LevelComplete.js(5,56): BCE0022: Cannot convert 'UnityEngine.MeshRenderer[]' to 'UnityEngine.GameObject[]'.

If anybody could point me in the right direction that would be great. Thanks for your help!

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

Answer by Cherno · May 03, 2015 at 04:25 PM

Ok, here is what you are doing wrong:

You declare that limbs is an array with elements that are of type GameObject. However, then you try to populate this array by calling

 GetComponents<MeshRenderer>()

, which returns an array with elements of type MeshRenderer. I think you see where the error is here. If you want to access the MeshRenderers of each GameObject in an array, populate the array with elements of the appropriate type, i.e. GameObjects. You can then use a simple foreach loop to cycle through the GameObjects in the array and use

 go.GetComponent<MeshRenderer>()

on each element to access their MRs. However, if you want an array all the MeshRenderer components that are in the scene, you need to change the limbs array's element type to MeshRenderer, and then you can populate it with

 var limbs : MeshRenderer[];
 
 limbs = FindObjectsOfType(typeof(MeshRenderer)) as MeshRenderer[];

You can then cycle to that array to access the MeshRenderers directly, and access the gameObjects they are attached to with

 foreach(MeshRenderer mr in limbs) {
      GameObject go = mr.GameObject;
 }


Comment
Add comment · Show 2 · 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 plsright · May 04, 2015 at 12:25 AM 0
Share

Thanks, this was extremely helpful!

avatar image Cherno · May 04, 2015 at 08:34 AM 0
Share

$$anonymous$$y pleasure. :)

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

20 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

Related Questions

Shotgun using array/list in Javascript 1 Answer

Creating a dialogue? 1 Answer

How can an object add itself to a scripts Array when it's instantiated? 1 Answer

Reading and Storing External Data into Memory (From Text) 1 Answer

Type 'Object' does not support slicing. 1 Answer


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