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 /
avatar image
0
Question by beco13 · Aug 05, 2011 at 09:55 AM · gameobjectarraymultiple

Is this doable in unity

I want to ask to all of you who understand Unity better than I do....please help me...

I have this a gameobject name "A" within this gameobject I've got a script named "Ascript"

We are able to get the script file using GetComponent() to change values within Ascript.

Usually if I wanted to have some other script with the same name which is "Ascript" I created other gameobject with other name that hold Ascript to access and manipulate values within the other Ascript.

Is it doable in unity3, if I had this game object "B" that holds two or three scripts named "Bscript" and access it dynamically.

in short : Can I access several components with the same type and same name within a game object. and change values within one script without interuptting other values?

please help me ^^ thx

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

2 Replies

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

Answer by Bunny83 · Aug 05, 2011 at 10:09 AM

Sure, that's why there are the functions GetComponents and GetComponentsInChildren.

 // C#
 Ascript[] myAscripts = GetComponents<Ascript>();
 myAscripts[0].someValue = 5;


Comment
Add comment · Show 3 · 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 beco13 · Aug 08, 2011 at 03:28 AM 0
Share

can you explain more details for your suggestions :)? is it having multiple scripts in the same game objects? So I'd have to assign an array to access different script with the same name?

Ascript[0] for myScript number 1 in gameObjectA Ascript[1] for myScript number 2 in gameObjectA is your script works that way??

avatar image beco13 · Aug 25, 2011 at 07:26 AM 0
Share

thanks for the answer,it helps me anyway :)

avatar image Bunny83 · Aug 25, 2011 at 01:11 PM 0
Share

Ohh, i've missed that comment, sorry.

Yes you're right GetComponents returns an array containing all scripts of that type that are attached to this gameobject ;)

avatar image
1

Answer by roamcel · Aug 05, 2011 at 10:07 AM

Your question is honestly pretty unclear, but if your question is: "can I have the same script attached multiple times to a single gameobject" the answer is YES, but it would possibly not work as expected UNLESS you create empty gameobjects that are childs of your original gameobject, and which you name differently. This way, your gameobject, called A, would have for example four childs called A1, A2, A3 and A4, to which you attach Ascript on each. This way, all scripts would be indipendent and easily addressable. To access Ascript in A4, you can simply issue a

A.FindChild("A4").GetComponent<Ascript>();
Comment
Add comment · Show 5 · 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 Bunny83 · Aug 05, 2011 at 10:14 AM 0
Share

You don't need to create child objects to attach a script multiple times to the same GameObject. "but it would possibly not work as expected" why do you think it wouldn't work as expected?

Of course it completely depends on the scripts task. It makes no sense to attach two Character$$anonymous$$otor script to the same object but i guess some common sense can be expected.

avatar image beco13 · Aug 08, 2011 at 03:25 AM 0
Share

@roamcel: your suggestions are the usual way I did it :) Thx for your suggestions, the way I want it to be is, to access those scripts in a gameobjects without interupting or having a malaccess of other script with the same name :)

avatar image Joshua · Aug 25, 2011 at 08:12 AM 0
Share

@Bunny83 how do you know which instance of the class you are referencing then?

avatar image Bunny83 · Aug 25, 2011 at 01:17 PM 0
Share

@Joshua: well, what's actually the difference between them? They both are instances of the same object. The order you get the components won't change. If you configured the scripts in a different way you can check for that. You can also offer a public variable to give the script a name / ID / whatever.

avatar image roamcel · Aug 25, 2011 at 02:23 PM 0
Share

"not work as expected" was simply directed to the fact that you usually don't need to do it BECAUSE you need an alternative way to differentiate which-same-script serves what purpose. Doing that with public variables and through code is certainly more counter intuitive and less efficient than doing it with prefabs.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Access a String array in one script from another script 0 Answers

Array of GameObjects created with a Script 2 Answers

how to build & populate an array available to all scripts on all objects? 1 Answer

Access variables, objects in array in another script? 2 Answers

How do I get each GameObject to mind its own business? 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