- Home /
refer to component by number or type instead of name?
i have a main script called main.js attached to a gameobject named main. I want to set variables in main from another script.
another script code:
var mainscript:main;
mainscript=GameObject.Find("main").GetComponent("main");
mainscript.somevariable=2;
I Update main.js a lot and change the name to main_1, main_2 to keep track of the versions I'm on. Is there a way to refer to main by the component type or number so I don't have to keep changing the name everytime?
Thanks,
You change the name of your type?? That's really asking for trouble. Try using some kind of version control ins$$anonymous$$d.
In any case, you should use the generic form of GetComponent:
var mainScript : main = GetComponent.<main>();
And really don't ever change your type-names without some kind of 'refactor' tool in your ID$$anonymous$$ It's way too tedious to go through every single time you use it and change all the names.
syclamoth is totally right. It's like your calling for problems later in your development.
However, this is as good a place as any to ask about typedef, is there any of those ?
I'm just using unitron and unity could you recommend a system I could explore for version control?
You shouldn't use the generic form of GetComponent when using Unityscript; GetComponent(main)
runs slightly faster than GetComponent.<main>()
and does the same thing. Just don't use strings.
But yeah, don't change the name of the script like that.
Answer by Alec-Slayden · Feb 29, 2012 at 09:33 AM
When you're making a new main script, write it as a class that extends Main. This way you can keep the functions you want, overwrite the ones you don't, and still have things look for Main.