- Home /
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
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;
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??
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 ;)
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>();
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.
@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 :)
@Bunny83 how do you know which instance of the class you are referencing then?
@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.
"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
Follow this Question
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