I want to be able to link one script to another in the same way you can access GameObjects or components from scripts:
If I put this at the top of my script this allows me to set the GameObject in the unity editor. How would I do the same thing for a script if I don't always know what it's name is? It could vary from object to object...
asked Jan 01 '11 at 04:48 PM
Alright, I see, okay, so, each object that has a damage receive script, has a function, that controls that objects health...
So, I'd suggest looking at Broadcast message, and use that, as you can "Broadcast" a message to that function, and change a value in it, being the health of the object, then that'll reduce the amount of health...
I think this is what you're looking for...
Here's a link to the API: http://unity3d.com/support/documentation/ScriptReference/GameObject.BroadcastMessage.html
That should help you, it might take a couple tests to get right, but hope it helps.
Have a good new year!
answered Jan 01 '11 at 05:35 PM
All scripts have to derive from MonoBehaviour, so you can do this:
However, I can't think of why you'd want to do that. Additionally, you have to drag a game object onto that slot, not a script from the project pane, because what the variable does, is hold a reference to a particular instantiated class. So, you can only get a reference to the first one attached to a Game Object, unless you set the variable in code.
Edit: Okay, I read your rationale for this. The above won't help you, but something very similar will. You just need to create a new class, that is derived from MonoBehaviour, that all the damage scripts also derive from.
First, create a base class for all of your damage scripts to derive from:
GetHurt is "abstract", in that it doesn't do anything here, but can be overridden in scripts that derive from it. You'll define what it does in the derived classes, so, for example:
To assign any type of Damage script, that is, anything that derives from it, you can create a variable that holds a Damage, like so:
and you can, for example, drag a MuppetDamage onto the damage variable slot, in the Inspector.
The quick and easy way is to use a string and just call GetComponent using it.
answered Jan 01 '11 at 05:26 PM