- Home /
How do you access a script if you don't know it's name?
I want to be able to link one script to another in the same way you can access GameObjects or components from scripts:
var myObject : GameObject;
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...
$$anonymous$$ight I ask, why would you not know the script name? Like, if you make the object, you know the scripts you put on it...
Well what I want is to make a universal 'damage' system for my game.
The player has a behaviour script which manages whether they are attacking and how much health they have.
When they attack a trigger is 'switched on'. This trigger has an 'attack script' which obtains values from the 'player behaviour' script.
The enemies in the game have 'damage receive' scripts. The trigger 'attack script' looks for these and if it finds them it runs a 'Damage' function in the 'damage receive' scripts. These 'damage receive' scripts pass the values and type of damage to the 'enemy behaviour' script...
... unfortunately these 'enemy behaviour' scripts change from one object to the next. So I need to be able to find a script that I don't know the name of in the enemy object and link the 'damage recieve' to the 'enemy behaviour'.
Answer by Justin Warner · Jan 01, 2011 at 05:35 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!
That's perfect :) solves the problem very neatly. Thank you very much and Happy New Year to you too! :D
I'm not knocking Justin's answer, because it is a solution, but I don't think the method is neat, and it's slow. It could work as a last resort if you don't care about performance.
Answer by Jessy · Jan 01, 2011 at 05:30 PM
All scripts have to derive from MonoBehaviour, so you can do this:
var script : MonoBehaviour;
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:
class Damage extends MonoBehaviour {
public function GetHurt () {};
}
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:
class MuppetDamage extends Damage {
function GetHurt () { lifePoints -= Mathf.Infinity; }
}
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:
var damage : Damage;
function Hurt () { damage.GetHurt(); }
and you can, for example, drag a MuppetDamage onto the damage variable slot, in the Inspector.
C# makes it more explicit, what you're doing, which I feel kills a lot of potential confusion, with abstract classes, but you can still do it.
Answer by tchen · Jan 01, 2011 at 05:26 PM
The quick and easy way is to use a string and just call GetComponent using it.
This doesn't help fully if you don't know the name of the script... That's kind of the problem, which I don't understand why you'd want this, and I don't think it's fully possible lol.
Hmm. Well the 'enemy' objects (see above) should know the name of their scripts. It's just the problem of passing that to the script that needs it. I think I can see how it would work...
Answer by tosha-perkins-hopkins · Apr 19, 2012 at 03:00 PM
how do you make a script if you don't want to because its due tody
Answer by tosha-perkins-hopkins · Apr 19, 2012 at 02:59 PM
how do you make a easy script when its due today