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 bimboladimeji · Jun 20, 2010 at 04:37 PM · componentreferencenamesame

Getting Components with the same name

I am trying to create an object that can be affected by more than one joint at run-time. When a new object B is attached to my original object A, A adds a configurable joint to itself and takes B as its connected rigid body. The problem occurs when a new object C wants to attach itself to the original object A. A adds a new configurable joint to itself, but when I use the GetComponent() function to get the configurable joint, it returns the first joint created so changing the connected body that joint replaces B (i.e object C replaces B) and the joint created for C cannot be accessed leaving the connected body to be none. How can I reference the joint created for C without replacing that for B.

PS: Sorry the question sounds like a riddle that's the best way I can explain it

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Mike 3 · Jun 20, 2010 at 04:44 PM

Use GetComponents instead, which returns an array you can loop through (or store the components yourself in an array when you do AddComponent, so you know which is which)

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 bimboladimeji · Jun 20, 2010 at 04:58 PM 0
Share

Thanks a lot!!!

avatar image Mike 3 · Jun 20, 2010 at 05:37 PM 0
Share

No problem, though please mark the answer as solved :)

avatar image
1

Answer by bimboladimeji · Jul 22, 2010 at 06:58 AM

For some reason gameObject.GetComponents(AudioSource) does not seem to return an array try casting whatever it returns into an array. This should work. Try this:

var mySfxAudioSources:AudioSource[];

function Start() { mySfxAudioSources = Array(gameObject.GetComponents(AudioSource)); }

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 Grimmy · Jul 22, 2010 at 10:49 AM 0
Share

That worked. Thanks!

avatar image
0

Answer by Grimmy · Jul 21, 2010 at 11:31 AM

All I ever get when I try this is

InvalidCastException: Cannot cast from source type to destination type. AudioManagerScript.Start () (at Assets\MIHT_Assets\MIHT_Scripts\AudioManagerScript.js:27)

my code is:

var mySfxAudioSources:AudioSource;

function Start() { mySfxAudioSources=gameObject.GetComponents(AudioSource); }

I'm obviously doing something wrong but I dont know what. I have tried declaring a new array length too(mySfxAudioSources=new AudioSource(20)) but I still get the same problem.

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 Mike 3 · Jul 21, 2010 at 12:44 PM 0
Share

needs to be var mySfxAudioSources:AudioSource[];

avatar image Grimmy · Jul 21, 2010 at 06:36 PM 0
Share

I tried that and that got rid of the error but my mySfxAudioSources array still shows up as length 0 in the inspector..even though my gameObject has 3 audio sources attached. Any idea why?

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

No one has followed this question yet.

Related Questions

Can I make reference/assocation to another component as variable? 0 Answers

Changing script name displayed in inspector 3 Answers

Inspector:Reference a component in Assets, not in scene. 0 Answers

Multiple scripts with the same name issue 1 Answer

Best practice for assigning class reference at runtime? 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