I thought this would be easy, but it's turning out to be a multi-hour stumbler. I'm basically trying to define a variable that points to a script of varying names.
Suppose I have 10 cubes. Every cube has a script attached (mainCubeScript.js). This script contains all the functions common to each cube. Now, every cube also has one unique secondary script attached (cubeAScript.js, or cubeBScript.js, or cubeCScript.js etc.), all similar scripts with variations to give each cube unique behaviors providing different flavors of functions needed in mainCube. Every one of those scripts has identical function names, with varying code within. Now, what I really want, is for gameCubeScript to talk to the one secondary script attached which varies in name.
My challenge is to reference functions and variables in the scripts using a single handle. So the code in mainCubeScript.js might be:
The typical examples in the documentation (var hCube = GameObject.Find("cubeAScript");) only work because the variable hCube is dynamically typed, but hence it has no script-wide scope. I have to prototype the variable hCube before using it because many functions in mainCube will need to access it. The problem is therefore that Unity then forces me to specify a variable type, which must be a specific script name. My challenge is that pesky prototype line: "var hCube : cubeAScript;" which prevents hCube from ever being linked to anything but cubeAScript.
It has been recommended in other variations of this question that I use "var hCube : MonoBehavior;" but then I get the errors "showID() is not a member of MonoBehaviour". It's been recommended also that I could make cube?Script subclasses of a master class, but then I get problems with some of the class functions and subclass functions getting ignored (different topic). Let's keep the focus of this question narrow: is there any way to prototype the variable hCube so that I can assign it by variable to different scripts as shown in the code above? Let's also assume that we simply need to structure the scripts this way without exploring why. All help welcome. Thanks!
You can create an Interface containing the function defenitions of your cube classes, so for example:
Then you can retrieve your object by the following code:
And now on you can call every method that is defined in this interface.
answered May 20 '11 at 08:35 AM
That sounds like quite a pesky problem... how about declaring your variable in line? I'm not sure if Unity will spit back that the variable has already been defined... but you could try something along these lines:
Alternatively, have you thought about putting these scripts into a single cubeScript? If you have shared functions with just small changes to behaviour it may be less of a headache all round?
answered May 20 '11 at 08:34 AM
This is my present solution, in mainCubeScript:
Each cube then gets assigned mainCubeScript and one of the cube?Scripts. Unity doesn't generate any errors about the missing scripts (9 of 10 of the references are null) because they are not used at run time.
I added switch statements in the main script only. The reason I have kept the secondary scripts separate is that I'm not really working with cubes, but rather creatures of moderate complexity. Each secondary script links to many specific objects (creature parts) and manipulates them uniquely (widely different anatomies), yet all creatures share some common behaviors such as going to a place. Now in retrospect, I should've created classes, but alas, perhaps this exercise helps others contemplating this approach.
answered May 20 '11 at 09:46 PM
you can name the scripts the same name as your cubes and then GetComponent(cube.name) or use a variable to differentiate the cubes and use GetComponent("CubeScript" + var).
Those are workarounds, for your case, I really suggest you make one script with all the variations and use a switch, or if you're using C# make an interface.
As Erich5h5 stated, you can simply solve it using a Sendmessage ot acces the method in whatever the script is named too, although it is slower than using getcomponent. http://answers.unity3d.com/questions/16061/whats-the-difference-between-sendmessage-and-getco.html
answered May 20 '11 at 03:39 PM