|
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...
(comments are locked)
|
|
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
Jan 01 '11 at 05:43 PM
Sydan
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.
Jan 01 '11 at 05:53 PM
Jessy
(comments are locked)
|
|
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. Is that for C only or also Java?
Jan 01 '11 at 05:32 PM
Justin Warner
Neither C nor Java are used in Unity.
Jan 01 '11 at 05:33 PM
Jessy
...Javascript or C#, sorry...
Jan 01 '11 at 05:39 PM
Justin Warner
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.
Jan 01 '11 at 05:50 PM
Jessy
(comments are locked)
|
|
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.
Jan 01 '11 at 05:29 PM
Justin Warner
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...
Jan 01 '11 at 05:31 PM
Sydan
(comments are locked)
|
|
how do you make a script if you don't want to because its due tody
(comments are locked)
|
|
how do you make a easy script when its due today
(comments are locked)
|

Might 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'.