- Home /
Multiple scripts with the same name issue
Hi,
I have a gameobject (VisualizerManager) with multiple scripts of the same name (VisDataGroup), and I am having problems targeting the correct script in JavaScript. It seems the Unity searches through them in order and finds the first one.
I cannot rename the scripts as they are part of the Visualiazer Studio component.
Each script does have a value in it of DataGroupName which can be used to distinguish between them, their unique values are: Bass, Mid, High.
Is this the best route to go to try and target the right script?
Currently I target the first one successfully like this:
thevisualizermanager.GetComponent("VisDataGroup").boost = 2;
"boost" is the value I am changing in them. I need to change them all to different values so I need to be specific about which one.
Any ideas and pointers in the right direction will be much appreciated.
Thanks.
Paul
Answer by Dragonlance · Aug 09, 2012 at 05:36 PM
Hi UFO Hunter,
what about
VisDataGroup[] visDataGroups = thevisualizermanager.GetComponents<VisDataGroup>();
foreach (VisDataGroup visDataGroup in visDataGroups) {
// Check the Id here and act accordingly
}
Hey Dragonlance,
Thanks for the suggestion. However it gives me 9 errors for all lines. For example: expected. Insert a semicolon at the end.
Unexpected token: )
expecting ), found 'visDataGroup'
expecting EOF, found '}'
I've not used foreach commands before, so I'm not sure about the correct syntax to use, but will go and research it now.
The theory looks sound, though. Create an array of your VisDataGroup components, and then you have a way to identify each one.
I haven't used foreach before, though. I'd use a regular for loop.
Thanks Screenhog. I have not been able to get this working and may leave it for a while. Can someone suggest how I should deal with the answers given as I can seem to be able to confirm them.
UFO maybe it is because my snippet is in C# and you are working in javascript. You will have to adapt it.
Sorry for that, but I did not see that you are only into javascript from your post.
var visDataGroups : VisDataGroup[];
visDataGroups = gameObject.GetComponents (VisDataGroup);
for (var visDataGroup : VisDataGroup in visDataGroups ) {
// Check the Id here and act accordingly
}
Your answer
Follow this Question
Related Questions
beginner question- several errors 1 Answer
Getting a scene object's name 1 Answer
How do I use current List Object [i] as the string name? 3 Answers
Using a Variable over two Scripts 4 Answers
As I can assign a script to an object using JavaScript? 2 Answers